How Do I Add Overrides to a form?

  • Thread starter Thread starter A Wieser
  • Start date Start date
A

A Wieser

I created a new Windows Forms Application, with a blank form.

The first thing I want to do is override OnPaint, and draw a line across the
window.

I can't for the life of me figure out how I'm supposed to do this with
Visual Studio.

If I go to the class view, and click on form1 a bunch of properties show up
for the form, but if I click on either the events button in the properties
window, or the Messages or the Override button, the pane is blank.

Right clicking the class name in Class View allows me to Add, but I have to
figure out the prototype by hand.

If I click on the form in design view, all of the events show up in the
properties window, but I still don't get any overrides, despite the button
being enabled.

Is there a way to do this (there must be surely). VC 6 made this so easy.

Please, someone help!

Tony
 
A said:
The first thing I want to do is override OnPaint, and draw a line across the
window.

I can't for the life of me figure out how I'm supposed to do this with
Visual Studio.

You have to write the code on your own. Something like this:

protected:
virtual void OnPaint(
/* whatever arguments the method has */
) override;
If I click on the form in design view, all of the events show up in the
properties window, but I still don't get any overrides, despite the button
being enabled.

That's not an override, but an event handler. If you double click on an
event, the IDE genearates an empty event handler for you automatically.
An event handler is not an override to a virtual function.

Typically when you create custom controls, you subclass the control,
creating your own class, and override the required virtual method.
However, when you design an application form, you simply handle the
events of the controls on the form.

For example, a Button is capable of firing the Click event, without
knowing which method is going to handle it. All you have to do is double
click on the Click event, and the IDE binds your Button1Click handler
method to the Button1->Click event.

In your particular case, it doesn't really make a difference if you
handle the Paint event, or override the OnPaint virtual method. You'll
get the same result. Just go to the Properties pane, switch to event
mode, and double click on the Paint event in order to handle it.

Tom
 
| > The first thing I want to do is override OnPaint, and draw a line across
the
| > window.
| >
| > I can't for the life of me figure out how I'm supposed to do this with
| > Visual Studio.
|
| You have to write the code on your own. Something like this:

Thanks for the reply. I had hoped the IDE gave some support here, as the
signatures are known by Intellisense.
<update>
It turns out that the object browser allows you to copy the nearly correct
definition (without the virtual, override, and the ^ on the arguments)
</update>

|
| protected:
| virtual void OnPaint(
| /* whatever arguments the method has */
| ) override;
|
| > If I click on the form in design view, all of the events show up in the
| > properties window, but I still don't get any overrides, despite the
button
| > being enabled.
|
| That's not an override, but an event handler. If you double click on an
| event, the IDE genearates an empty event handler for you automatically.
| An event handler is not an override to a virtual function.

There's both an events button (with a lightning icon) and a greenish shoebox
two icons to the right of it that has a tooltip labelled Overrides. That's
the one I was hoping would work. It's also surprising that the events are
only visible in design mode.

| In your particular case, it doesn't really make a difference if you
| handle the Paint event, or override the OnPaint virtual method. You'll
| get the same result. Just go to the Properties pane, switch to event
| mode, and double click on the Paint event in order to handle it.

I've now implemented half the drawing in both, just to see what happens.
How are you supposed to choose?

I also notice that when I resize my screen the client area is not
invalidated. It used to be possible to modify the window style at creation
to include CS_VREDRAW and CS_HREDRAW. Petzold hints at a ResizeRedraw
property, but it doesn't seem to be in the form class.

Finally, how do I call the base class in the OnPaint method without getting
carpal tunnel syndrome?
In C#, VB, or J# there's base.OnPaint(pe); MyBase.OnPaint(pe), or
super.OnPaint(pe);
Do I really have to type the following?:
System::Windows::Forms::Form::OnPaint(pevent);

Tony
 
A said:
Thanks for the reply. I had hoped the IDE gave some support here, as the
signatures are known by Intellisense.

It would be nice if the IDE could generate an empty override
placeholder. I'm not sure if it can do that. However, if you press
Ctrl+Space after typing "OnPaint" (or even "On"), the IDE lists the
possible methods, and when you open the parenthesis, it shows you the
argument list.
I've now implemented half the drawing in both, just to see what happens.
How are you supposed to choose?

The implementation itself is nearly identical in both cases. I would
personally use events when developing an application, and overrides when
developing a reusable component/control.
I also notice that when I resize my screen the client area is not
invalidated. It used to be possible to modify the window style at creation
to include CS_VREDRAW and CS_HREDRAW. Petzold hints at a ResizeRedraw
property, but it doesn't seem to be in the form class.

You still have low level access to the CreateParams:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.createparams(d=ide).aspx

However, I recommend using the SetStyle method:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx

What I think you need is ResizeRedraw: "If true, the control is redrawn
when it is resized." (from
http://msdn2.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx)
Finally, how do I call the base class in the OnPaint method without getting
carpal tunnel syndrome?
In C#, VB, or J# there's base.OnPaint(pe); MyBase.OnPaint(pe), or
super.OnPaint(pe);
Do I really have to type the following?:
System::Windows::Forms::Form::OnPaint(pevent);

__super::OnPaint(pevent);
http://msdn2.microsoft.com/en-us/library/94dw1w7x.aspx

Tom
 
Back
Top