Form Inheritance

G

Guest

I have a BASE form that I've inherited to another form. How can I override a
button that is on the BASE form. For example, I have a button named btnPrint
on the BASE form that has a default event setup, but on the form that
inherited the BASE form I want to change what the btnPrint button will do. In
other words, I want to OVERRIDE what the BASE form has setup for it.
 
G

Guest

I believe that the recomended way to this is as follows. In the base form,
have the btnPrint click event call a routine OnBtnPrintClick, which you make
overrideable. Then in the derived form, override it.
 
A

Armin Zingler

Greg said:
I have a BASE form that I've inherited to another form. How can I
override a button that is on the BASE form. For example, I have a
button named btnPrint on the BASE form that has a default event
setup, but on the form that inherited the BASE form I want to change
what the btnPrint button will do. In other words, I want to OVERRIDE
what the BASE form has setup for it.

Base Form:

Protected Overridable Sub OnButtonClick( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click

'whatever

End Sub

Derived Form:

Protected Overrides Sub OnButtonClick( _
ByVal sender As Object, ByVal e As System.EventArgs)

MyBase.OnButtonClick(sender, e) 'optionally (depends)

End Sub



Armin
 
G

Guest

Armin,
Have you tried this method? The documentation I have says you can't
override an event the same as you can a method. Hense the technique I
described earlier.
 
A

Armin Zingler

Terry said:
Armin,
Have you tried this method?

Yes. It worked.
The documentation I have says you can't
override an event the same as you can a method. Hense the technique I
described earlier.

I did not override an event. I override an event handler, which is a method.


Armin
 
G

Guest

Hmmm, you are right of course. I had to try it to prove it to myself. Think
this is the first time that I have been mislead by Mr. Balena's book. Thanks
for the info.
 
J

Jialiang Ge [MSFT]

Hello,

According to your description, you want to inherit a button's event in the
base form. If I am off base, please feel free to let me know.

Here is a MSDN article that talks about Events and Inheritance:
http://msdn2.microsoft.com/en-us/library/aa645739(VS.71).aspx
Please refer to the "Events and Inheritance" section.

Just as other community members suggested, we could either declare the
invoking method as virtual, which allows the derived class to override it,
or we could create a protected invoding method for the event. By calling
this invoking method, derived classes can invoke the event.

Please feel free to let me know if you have any other concern or need
anything else.

Sincerely,
Jialiang Ge ([email protected], remove ¡®online.¡¯)
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box ¡°Tools/Options/Read: Get 300 headers at a time¡±
to see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided ¡°AS IS¡± with no warranties, and confers no
rights.
 
G

Guest

I gave this a try and yes, it works. The new button overrides the other base
form's button event. But, the only problem is my derived form runs the event
twice for some reason. Is there a reason why this would be happening? I've
checked very closely, and the button does not have two identicle events, plus
in break mode, the event intself actually runs twice. Any ideas why this is
happening? And, any idea how I can stop this form happening?

Thanks
 
G

Guest

Here's an additional piece of information I forgot to included in my previous
reply.

This is the event on my Derived form. This is the event that gets called
twice.

Protected Overrides Sub OnButtonPrint_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnReject.Click
MessageBox.Show("Part Rejects " & i)
End Sub

Then, my base for looks like this.

Protected Overridable Sub OnButtonPrint_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnReject.Click
Dim frm As New frmOrderPartReject
frm.WindowState = FormWindowState.Maximized
frm.MdiParent = Me.MdiParent
frm.Show()
End Sub

So, like I say, my new event works fine, except its running twice for some
reason.

Thanks.
 
A

Armin Zingler

Greg said:
Here's an additional piece of information I forgot to included in my
previous reply.

This is the event on my Derived form. This is the event that gets
called twice.

Protected Overrides Sub OnButtonPrint_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnReject.Click
MessageBox.Show("Part Rejects " & i)
End Sub

Then, my base for looks like this.

Protected Overridable Sub OnButtonPrint_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnReject.Click
Dim frm As New frmOrderPartReject
frm.WindowState = FormWindowState.Maximized
frm.MdiParent = Me.MdiParent
frm.Show()
End Sub

So, like I say, my new event works fine, except its running twice
for some reason.

Thanks.

You don't need the "Handles" in the derived Form. Otherwise, the event
handler is attached twice. "OnButtonPrint_Click" of the current instance
handles the event because it's overriden. If it's an instance of the base
form, the base Form's method handles the event, otherwise it's handled by
the overriden method in the derived form.


Armin
 
J

Jialiang Ge [MSFT]

Hello Greg,

If you need further assistance, please feel free to let me know. I will be
more than happy to be of assistance.

Have a great day!

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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