Some basic graphic

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

In the C-tor I try to draw a red line from location 1,1 with width 20 to
location 100,100 but nothing is being drawn when the code is being executed.
So there must be somthing that I have missed here.
In the MCTS book 70-536 is says "To run this code, create a Windows forms
application and add the code to a method run during the form's Paint event."

I must say that graphics is very new to me so my knowledge is very limited.

public Form1()
{
InitializeComponent();
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Red, 20);
g.DrawLine(pen, 1, 1, 100, 100);
}

//Tony
 
Tony Johansson said:
Hi!

In the C-tor I try to draw a red line from location 1,1 with width 20 to
location 100,100 but nothing is being drawn when the code is being
executed.
So there must be somthing that I have missed here.
In the MCTS book 70-536 is says "To run this code, create a Windows forms
application and add the code to a method run during the form's Paint
event."

I must say that graphics is very new to me so my knowledge is very
limited.

public Form1()
{
InitializeComponent();
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Red, 20);
g.DrawLine(pen, 1, 1, 100, 100);
}

//Tony

It works now I didn't saw the paint event for the Form class the first time
I looked.

//Tony
 
You should use the paint event, not the constructor, since the constructor
is called once, while the paint event is used when the window need to be
repainted (for any reason). As example, if another form slide over yours,
and slide back, the paint event is likely to be called, and your nice code
drawing the ellipse, made in the constructor, won't be used at all,
resulting in its absence, after the new paint. That is probably not what you
want.


Vanderghast, Access MVP
 
vanderghast said:
You should use the paint event, not the constructor, since the constructor
is called once, while the paint event is used when the window need to be
repainted (for any reason). As example, if another form slide over yours,
and slide back, the paint event is likely to be called, and your nice code
drawing the ellipse, made in the constructor, won't be used at all,
resulting in its absence, after the new paint. That is probably not what
you want.


Vanderghast, Access MVP

Yes I know. I used to override the OnPaint method that exist in the Control
class.
//Tony
 
Back
Top