Problem with the event in inherited form

J

JC Voon

Hi:

My base form has a button, when click it will call MessageBox.Show(
"Base form" ). I inherite a child form from the base and assign a
button click event to the same button which will call MessageBox.Show(
"Inherited form" ). I run my application, and load the clild form,
click the button it show both message !!!
Is there any way to control the event sequence so that it only show
the child form event, or i can control whether to trigger the base
form event or not.

Thanks
JCVoon
 
G

Guest

In the base form class, mark the routine as Overridable:

Public Overridable Sub Button1_Click(...) Handles ...

In the inherited class

Public Overrides Sub Button1_Click(...)

Do not use the Handles statement in the inherited form.

To get the base form code to process, add this code to the event handler in
the inherited form:

MyBase.Button1_Click(Nothing, Nothing)

www.charlesfarriersoftware.com
 
H

Herfried K. Wagner [MVP]

JC Voon said:
My base form has a button, when click it will call MessageBox.Show(
"Base form" ). I inherite a child form from the base and assign a
button click event to the same button which will call MessageBox.Show(
"Inherited form" ). I run my application, and load the clild form,
click the button it show both message !!!

You may want to remove the handler added in the base class in the derived
class:

\\\
RemoveHandler Me.Button1.Click, AddressOf MyBase.Button1_Click
///
 
G

Guest

Hi JC

I would deal with this by having a Protected Overridable method in your base
class, and in your event handler, call this method. Then in the derived
class, override the method.

Base Form:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
ClickThing()
End Sub

Protected Overridable Sub ClickThing()
MessageBox.Show("base")
End Sub

Derived Form:
Protected Overrides Sub ClickThing()
MessageBox.Show("derived")
End Sub

HTH

Nigel Armstrong
 
J

JC Voon

Charlie, Herfried K. Wagner, Nigle Armstrong:

Thank for the reply. I've choose Charlie approch.
Is there any way to change the way IDE generate event ? If every event
is generate using public instead of private, it will save a lot of
work.

Cheers
JCVoon
 

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