Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I'm trying to build a generic event handler like this one :

 

Private sub myEventHandler(ByVal sender as System.Object, e as System.Eventargs) handles Textbox1.Enter, TextBox2.DoubleClick, Button1.Click

'Let's do some stupid stuff

msgbox (sender.name & " has fired event " & e.GetType.toString)

End sub

 

As you may guess, i'd like to know wich event has been fired for any sender hooked by myEventHandler.

 

I've parsed the Eventargs class documentation, but i ca'nt find any property returning the "name" of the event.

 

Any ideas ?

 

Thnks in advance,

Jean-Philippe

  • Leaders
Posted

   Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
       Button6_Click(sender, e)
   End Sub

   Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
       MessageBox.Show(sender.Name)
   End Sub

in that case the messagebox would show "Button5" if you raised the Button6_Click event from Button5.

hope that helps.

Posted

I already got the sender.name !

 

I'm just looking for some kind of "e.name" to mention that either Enter or DoubleClick have been fired...

 

Thanks anyway.

J.Ph.

Posted

I'm afraid to tell you...

 

There is NO particular reason ! Please forgive me !;-)

 

I'm just on my way to migrate from VB6 to .NET, and trying to undertand the underlying logic of it.

I just guess (?!) that replacing control arrays with generic handlers should permit a more flexible way to handle different events in the same place...

  • Leaders
Posted

   Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
       If TypeOf e Is MouseEventArgs Then
           MsgBox("mouse event")
       ElseIf TypeOf e Is EventArgs Then
           MsgBox("eventargs")
       End If

   End Sub

that will tell you what type of event triggered "e" , eg:

if i clicked a textbox ( using Mouse_Up ) it returned "mouse event"

if i used a button_Click ( System.EventArgs ) it returned "eventargs" :)

  • Administrators
Posted

Generic handlers can be useful if you have one handler for multiple events (Save button, save menu etc) that all require the same end result.

 

They can also be very useful when similar logic is required for multiple events (Text1.Changed, Text2.Changed etc) as then a single handler can cover most of the logic by using the sender object.

 

If one handler is trying to do too much work though it can easily end up as a monsterous Select Case statement trying to call the correct bit of code based on multiple parameters (bit like handling windows Messages in a C application)

 

Also one event can have multiple handlers, so a single button click could cause 2 or more event handlers to fire...

 

If it makes sense to use one handler - do so :)

If it makes sense to use more than one - do so :)

If it makes code harder to read or maintain - probably don't do so ;)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

>> Dynamic_Sysop :

your piec of code is better than what I've already found...

...but I still can't get the difference between "click" and "double click"...

 

>>PlausiblyDamp :

I do agree with you concerning code hard to read or maintain, it certainly will will not make life easier when used for handling 15 different events...

But let's says you have to trap focus(in & out) and keystroke on a textbox.

Does it make sense to add just one handler to catch what happens ? I do think so...

 

I have to leave now.

Have a nice evening, folks !

  • *Experts*
Posted

DynamicSysop, i dont get the logic of your sample :). It says that e is EventArgs, becuase thats whats passed in, why do you try to check what it is?

:)

Posted

MouseEventArgs inherits from EventArgs.

 

Thus any MouseEventArgs can be used, where EventArgs are required. Still the "original" type will not be lost.

.nerd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...