I would like to have some of my painting done in design time as well as in run time (for example, just to draw a simple line). Which method should I overload, or what event should I catch to achieve this?

I would like to have some of my painting done in design time as well as in run time (for example, just to draw a simple line). Which method should I overload, or what event should I catch to achieve this?

ill show you the two ways to do this.

first way is on form_Paint(in event properties under appearance) to use the PaintEventArgs to create the graphics object used to draw. This is best to use for something that must remain on the form, like text, because anytime the form is refreshed it is redrawn.

private void form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{

			Graphics g = e.Graphics;
			Rectangle rec = new Rectangle(1,1,20,20);

			g.DrawRectangle(new Pen(Color.Red, 8f), rec);//draws 20x20 red rectangle at (1,1)
			
		}

the second way is creating a new graphics object. this would be best used for drawing things that arnt static, like effects, because unless this its made to it will get cleared if the window is minimized or resized.

private void MyBox()
		{
			Graphics g = this.CreateGraphics();

			Rectangle rec = new Rectangle(1,1,20,20);

			g.DrawRectangle(new Pen(Color.Red, 8f), rec);//draws 20x20 red rectangle at (1,1)
		}

there is little difference between them, but i thought i would show you how to do both.

This article has been dead for over six months. Start a new discussion instead.