[.net] Parent and child user controls

Started by
-1 comments, last by jamesbf 17 years, 11 months ago
Hi Everyone, I have a question about the correct way to create a custom control that contains a dynamically controlled number of child controls. Basically i'm trying to implement a basic vector drawing control similar to that found in photoshop/xara/inkscape etc. I have a parent "vector" control that containes a number of "point" controls and "line" controls (where the lines connect the points). I figured this was the best way to do it so I could handle events on each part of the vector itself easily. The problem I have is with the OnPaint(PaintEvenArgs e) overrides. I seem to be able to add my child controls ok, and the OnPaint() functions are definately called for each child control, the problem is that after the first child control is finished e.graphics seems to be set to some arbitrary value for all other control's events. Because of this, cliprect is set to 0 and no child controls except the first added to the parent are drawn. I was wondering if someone could point me to a good example/tutorial of what im trying to do (i cant seem to find one anywhere), or maybe give me some hints as to what i'm doing wrong... I have pasted some relevant code below, just in case anyone is interested. Thanks in advance, James All control classes are declared this way
public class VectorControl : System.Windows.Forms.UserControl

Adding the parent (Vector) control to the main form
		public MotionForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			VectorControl vector1 = new VectorControl(40,40,20,20);
			this.Controls.Add(vector1);
		}

Adding the child controls to VectorControl
		public VectorControl(int x1, int y1, int x2, int y2)
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			PointList = new ArrayList();
			LineList = new ArrayList();
			
			//Add the requested start and end points to the line
			PointList.Add(new PointControl(x1,y1));
			PointList.Add(new PointControl(x2,y2));
			this.Controls.Add((PointControl)PointList[0]);
			this.Controls.Add((PointControl)PointList[1]);
			
			//And add the line itself
			LineList.Add(new LineControl(x1,y1,x2,y2));
			Controls.Add((LineControl)LineList[0]);

		}


PointControl OnPaint
	protected override void OnPaint(PaintEventArgs e)
		{
			//Update pen colours in case they changed
			PointPen.Color = foregroundColour;
			PointBrush.Color = backgroundColour
			
e.Graphics.DrawRectangle(PointPen,this.Point.X-(PointSize/2),this.Point.Y - (PointSize/2),PointSize,PointSize);
			if(Selected)
       e.Graphics.FillRectangle(PointBrush,this.Point.X-(PointSize/2),this.Point.Y - (PointSize/2),PointSize,PointSize);

			base.OnPaint (e);
		}

LineControl OnPaint
		protected override void OnPaint(PaintEventArgs e)
		{
			//Update line colours in case they've changed
			SelectedLinePen.Color = foregroundColour;
			NormalLinePen.Color = foregroundColour;
			
			if(Selected)
                e.Graphics.DrawLine(SelectedLinePen,StartPoint,EndPoint);
			else
			e.Graphics.DrawLine(NormalLinePen,StartPoint,EndPoint);

			base.OnPaint (e);

		}

This topic is closed to new replies.

Advertisement