Event Handler Fires Twice

G

Guest

I'm using VB.Net 2005

I have a Base Form that has a button control on it named btnEnter. The event
handler for this control is shown below:

Overridable Sub btnEnter_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnEnter.Click
Do Something....
End Sub

Then, this form is inhertied to another form named frmOrderProcessing. But,
on this form I want to perform a different event than what the base form is
doing. So, for the btnEnter button I create the following event.

Overrides Sub btnEnter_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnEnter.Click
MessageBox.Show("Do Something")
End Sub

Now, the click event on my second form runs correctly, except that it runs
twice. Why would this even fire twice and what must I do to make it work
correctly?

Thank You.
 
G

Guest

remove the 'Handles btnEnter.Click' on the derived form. You have
'subscribed' to the event twice. You only need to override the event
handler, not subscribe to the event a second time.
 
A

Armin Zingler

Greg said:
I'm using VB.Net 2005

I have a Base Form that has a button control on it named btnEnter.
The event handler for this control is shown below:

Overridable Sub btnEnter_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnEnter.Click
Do Something....
End Sub

Then, this form is inhertied to another form named
frmOrderProcessing. But, on this form I want to perform a different
event than what the base form is doing. So, for the btnEnter button
I create the following event.

Overrides Sub btnEnter_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnEnter.Click
MessageBox.Show("Do Something")
End Sub

Now, the click event on my second form runs correctly, except that
it runs twice. Why would this even fire twice and what must I do to
make it work correctly?

As I've already said in your previous thread:
You don't need the "Handles" in the derived Form.


Armin
 
P

Phill W.

Greg said:
I have a Base Form that has a button control on it named btnEnter. The event
handler for this control is shown below:

Overridable Sub btnEnter_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnEnter.Click
Do Something....
End Sub

Then, this form is inhertied to another form named frmOrderProcessing. But,
on this form I want to perform a different event than what the base form is
doing. So, for the btnEnter button I create the following event.

Overrides Sub btnEnter_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnEnter.Click
MessageBox.Show("Do Something")
End Sub

Now, the click event on my second form runs correctly, except that it runs
twice. Why would this even fire twice and what must I do to make it work
correctly?

Two "Handles" clauses; two sets of events.

Events and Inheritance (of Controls) often causes confusion like this,
which is why Our Friend in Redmond have a /different/ model for doing
with this, which goes something like this:

Class Base

' The class now has its own version of the EnterClick event
' This replaces the one coming from btnEnter itself
Public Event EnterClicked( _
sender as Object _
, e as EventArgs _
)

' This routine (and ONLY this routine) raises the event
Protected Overridable Sub OnEnterClicked( e )

' You can do things before raising the event ...

RaiseEvent EnterClicked( Me, e )

' ... and other things when the call comes back

' (You can even decide NOT to raise the event at all!)

End Sub

' a LOCAL event handler detects the actual button being clicked
Private Sub btnEnter_Click( _
sender as Object _
, e as EventArgs _
) Handles btnEnter.Click

' ... and raises the event from the enclosing class
Me.OnEnterClicked( e )

End Sub

End Class

In your derived class, you don't /want/ all the hassle of event handlers
(you want the base class to do that), but you still want to run your own
code when the button gets clicked. Easy enough; override the method
that raises the EnterClicked event.

Class Derived
Inherits Base

Protected Overrides Sub OnEnterClicked( e as EventArgs )
' Do something different.

' If you want to raise the EnterClicked event
' (to other code still waiting for it)
MyBase.OnEnterClicked( e )
End Sub

End Class

HTH,
Phill W.
 

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