Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

Event Handler Fires Twice

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 30 Oct 2007 19:37 GMT
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.
Terry - 30 Oct 2007 19:48 GMT
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.
Signature

Terry

> I'm using VB.Net 2005
>
[quoted text clipped - 20 lines]
>
> Thank You.
Armin Zingler - 30 Oct 2007 20:04 GMT
> I'm using VB.Net 2005
>
[quoted text clipped - 19 lines]
> 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
Phill W. - 31 Oct 2007 15:49 GMT
> I have a Base Form that has a button control on it named btnEnter. The event
> handler for this control is shown below:
[quoted text clipped - 16 lines]
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.