Some basic graphic

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
 
T

Tony Johansson

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
 
V

vanderghast

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
 
T

Tony Johansson

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
 

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

Top