why doesn't this DrawLine code do anything?

  • Thread starter Thread starter RichGK
  • Start date Start date
R

RichGK

This is from

http://msdn2.microsoft.com/en-us/library/aa287522(VS.71).aspx

public MainForm()
{
InitializeComponent();


System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 500, 500);
myPen.Dispose();
formGraphics.Dispose();
}

Sorry about the formatting, google is terrible at allowing tabs.
 
Hi,

actually the code does something, but the Line is deleted while the next
Paint event.
You should put this code into the OnPaint method.
 
a paint event erase what you draw in the contructor

public Form1()
{
InitializeComponent();
Paint += new PaintEventHandler(Form1_Paint);
}
void Form1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 500, 500);
myPen.Dispose();
formGraphics.Dispose();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top