I’ve had a look around the web regarding this, but haven’t found the exact answer I’m looking for or I’ve tried what is suggested and it doesn’t work!
I’m having issues in that I have a screen which has approximately 72 Checkboxes on it in a matrix which I have connected together using lines the coordinates of which I store in a list.
To draw the lines I use the Drawline method in an override method for OnPaint to iterate through the list as follows :-
protected override void OnPaint(PaintEventArgs e)
{
Pen myPen = new Pen(System.Drawing.Color.Black);
Graphics g = this.CreateGraphics();
myPen.Width = 5;
foreach(ConnectionLine cl in connectionLines)
{
g.DrawLine(myPen, cl.xStart, cl.yStart, cl.xStop, cl.yStop);
}
myPen.Dispose();
g.Dispose();
}
The strange thing about this is that it doesn’t appear to be the lines that take the time to draw – it’s now the checkboxes, if I remove the line functionality these refresh in the blink of an eye.
Any ideas much appreciated.
Thanks,
Dave
Part of the problem could be that you are recreating the Graphics object each time the control is painted. Instead you should use the
e.Graphicsobject that is provided inPaintEventArgs. You could also try using only one instance of Pen.