Kaskooye Posted July 1, 2003 Posted July 1, 2003 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 Quote
Leaders dynamic_sysop Posted July 1, 2003 Leaders Posted July 1, 2003 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. Quote
Kaskooye Posted July 1, 2003 Author Posted July 1, 2003 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. Quote
Administrators PlausiblyDamp Posted July 1, 2003 Administrators Posted July 1, 2003 I don't think there is a way to find out what kind of event fired, is there any particular reason why you would want to handle different event types for multiple controls in a single event handler? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Kaskooye Posted July 1, 2003 Author Posted July 1, 2003 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... Quote
Leaders dynamic_sysop Posted July 1, 2003 Leaders Posted July 1, 2003 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" :) Quote
Administrators PlausiblyDamp Posted July 1, 2003 Administrators Posted July 1, 2003 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 ;) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Kaskooye Posted July 1, 2003 Author Posted July 1, 2003 >> 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 ! Quote
*Experts* mutant Posted July 1, 2003 *Experts* Posted July 1, 2003 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? :) Quote
Heiko Posted July 1, 2003 Posted July 1, 2003 MouseEventArgs inherits from EventArgs. Thus any MouseEventArgs can be used, where EventArgs are required. Still the "original" type will not be lost. Quote .nerd
*Experts* mutant Posted July 1, 2003 *Experts* Posted July 1, 2003 Oh i missed a little part of that code :rolleyes:. Now that I look closer at the code I get it. btw. Cool avatar Heiko :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.