some basic question about which event handler to use

T

Tony Johansson

Hi!
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);
Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}


protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);

Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}

//Tony
 
P

Peter Duniho

Tony said:
Hi!
private void Form1_Paint(object sender, PaintEventArgs e)
{
[...]
}


protected override void OnPaint(PaintEventArgs e)
{
[...]
}

It's customary to include an actual question when you have a "basic
question". :)

Are you asking whether to use an event handler for the Paint event or to
override the OnPaint() method? The latter is not an "event handler" per se.

If that's the question, then as for the answer, this is mostly personal
preference, but MSDN recommends that if you can override the "On…"
method, you do so rather than subscribing to the event.

From a practical point of view, doing so gives you more control over
when your code runs as compared to any actual event handlers that may
exist, and has a very tiny, imperceptible improvement in call overhead
as compared to subscribing to the event instead.

Note that if you _do_ override the "On…" method, it's very important to
call the base implementation, which the example you posted does _not_.
Otherwise, any code that does subscribe to the event will not actually
get called.

Pete
 
T

Tony Johansson

Peter Duniho said:
Tony said:
Hi!
private void Form1_Paint(object sender, PaintEventArgs e)
{
[...]
}


protected override void OnPaint(PaintEventArgs e)
{
[...]
}

It's customary to include an actual question when you have a "basic
question". :)

Are you asking whether to use an event handler for the Paint event or to
override the OnPaint() method? The latter is not an "event handler" per
se.

If that's the question, then as for the answer, this is mostly personal
preference, but MSDN recommends that if you can override the "On…" method,
you do so rather than subscribing to the event.

From a practical point of view, doing so gives you more control over when
your code runs as compared to any actual event handlers that may exist,
and has a very tiny, imperceptible improvement in call overhead as
compared to subscribing to the event instead.

Note that if you _do_ override the "On…" method, it's very important to
call the base implementation, which the example you posted does _not_.
Otherwise, any code that does subscribe to the event will not actually get
called.

Pete

As you explined the Paint event is raised in the OnPaint method in the
Control class.
So because I override the OnPaint method my new implementation of OnPaint
will be called instead.
In the method OnPaint which I override as I mentioned I also do a
base.OnPaint as the last statement which should call all subscribers that
subscribe to the Paint event and in my example I do subscribe to the Paint
event but the Form1_Paint event handling method is not called.

You might have explained that but I haven't understood it yet.
So the question remains why is there no call to Form1_Paint event handling
method when I override OnPaint mehod as in my. Form1 class.

//Tony
 
T

Tony Johansson

Tony Johansson said:
Peter Duniho said:
Tony said:
Hi!
private void Form1_Paint(object sender, PaintEventArgs e)
{
[...]
}


protected override void OnPaint(PaintEventArgs e)
{
[...]
}

It's customary to include an actual question when you have a "basic
question". :)

Are you asking whether to use an event handler for the Paint event or to
override the OnPaint() method? The latter is not an "event handler" per
se.

If that's the question, then as for the answer, this is mostly personal
preference, but MSDN recommends that if you can override the "On."
method, you do so rather than subscribing to the event.

From a practical point of view, doing so gives you more control over when
your code runs as compared to any actual event handlers that may exist,
and has a very tiny, imperceptible improvement in call overhead as
compared to subscribing to the event instead.

Note that if you _do_ override the "On." method, it's very important to
call the base implementation, which the example you posted does _not_.
Otherwise, any code that does subscribe to the event will not actually
get called.

Pete

As you explined the Paint event is raised in the OnPaint method in the
Control class.
So because I override the OnPaint method my new implementation of OnPaint
will be called instead.
In the method OnPaint which I override as I mentioned I also do a
base.OnPaint as the last statement which should call all subscribers that
subscribe to the Paint event and in my example I do subscribe to the Paint
event but the Form1_Paint event handling method is not called.

You might have explained that but I haven't understood it yet.
So the question remains why is there no call to Form1_Paint event handling
method when I override OnPaint mehod as in my. Form1 class.

//Tony

It work now I had by mistake comment this row
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
So when I now do base.OnPaint(e); in the overridden OnPaint
the event handler Form1_Paint is called from the Control OnPaint method
which is correct.

//Tony
 
P

Peter Duniho

Tony said:
[...]
In the method OnPaint which I override as I mentioned I also do a
base.OnPaint as the last statement

You haven't posted any code for which that is true.
which should call all subscribers that
subscribe to the Paint event and in my example I do subscribe to the Paint
event but the Form1_Paint event handling method is not called.

Because your OnPaint() override does not call base.OnPaint().
You might have explained that but I haven't understood it yet.
So the question remains why is there no call to Form1_Paint event handling
method when I override OnPaint mehod as in my. Form1 class.

See above.

If that does not answer the question, post a concise-but-complete code
example that reliably reproduces the problem, and yet in which you do
correctly call the base.OnPaint() method.

Pete
 
T

Tony Johansson

Peter Duniho said:
Tony said:
[...]
In the method OnPaint which I override as I mentioned I also do a
base.OnPaint as the last statement

You haven't posted any code for which that is true.
which should call all subscribers that subscribe to the Paint event and
in my example I do subscribe to the Paint event but the Form1_Paint event
handling method is not called.

Because your OnPaint() override does not call base.OnPaint().
You might have explained that but I haven't understood it yet.
So the question remains why is there no call to Form1_Paint event
handling method when I override OnPaint mehod as in my. Form1 class.

See above.

If that does not answer the question, post a concise-but-complete code
example that reliably reproduces the problem, and yet in which you do
correctly call the base.OnPaint() method.

Pete

It works now!

Here is another question.
When I read the book 70-536 from Microsoft Press the book is using the
following.
method(marked 1) to create a Graphics object in the Paint event handler
method.
I think it's better to use method marked 2.
So what is the recommended way here between these two ?

private void Form1_Paint(object sender, PaintEventArgs e)
{
2 Graphics g = e.Graphics;
Pen pen = new Pen(Color.Maroon, 7);
g.DrawLine(pen, 1,1,100,100);
1 Graphics g = CreateGraphics();
}
 
P

Peter Duniho

Tony said:
[...]
When I read the book 70-536 from Microsoft Press the book is using the
following.
method(marked 1) to create a Graphics object in the Paint event handler
method.
I think it's better to use method marked 2.
So what is the recommended way here between these two ?

As has already been stated, using the Graphics instance from the
PaintEventArgs is the preferred method, _by far_.

It is extremely rare that a program should have to call CreateGraphics().

Pete
 
C

Chris Dunaway

Hi!
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);
Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Maroon, 0);
Brush b = new LinearGradientBrush(new Point(1, 1),
new Point(100, 100),
Color.White,
Color.Red);

Point[] points = new Point[]
{new Point(10,10),
new Point(10,100),
new Point(50,65),
new Point(100,100),
new Point(85,40)};
g.DrawPolygon(pen, points);
g.FillPolygon(b, points);
g.DrawPolygon(pen, points);
}

//Tony

Maybe this site will be helpful:

http://bobpowell.net/beginnersgdi.htm

Chris
 

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