Zebra0.com

csharp drawing

Drawing Lines

Drawing is done with either a Brush or Pen on a graphics object. The pen draws line, the brush draws filled areas.
You can not draw in form load because the Load event is run before the Form is drawn.
So anything you draw is overwritten by the Form.

Add this code to the MouseClick event to draw a line from the point clicked to the top left corner of the form.

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
   // Get the graphics of the form.
   Graphics g = this.CreateGraphics();
   // Create a Red Pen with a width of 3.
   Pen myPen = new Pen(Color.Red, 3); 
   // Draw a line from the point clicked to the top left corner.
   g.DrawLine(myPen, e.X, e.Y, 0, 0);
}
several red lines to top left corner

To Do: Experiment! Try drawing lines to each of the other corners.

End of lesson, Next lesson: ListBox Manager in C#