-
Mar 25th, 2014, 10:08 PM
#1
Thread Starter
Lively Member
Multiple Button Press Detect Event Handler
Hi,
I have a multiple button detection event handler that works - most of the time -
and I cannot figure out why it's not working for certain buttons specified.
Code:
Private Sub AnyButton_Click(sender As Object, e As EventArgs) Handles reqEnets.Click, reqAbout.Click, reqClrForm.Click, reqCloseDesign.Click, reqFindComps.Click, reqFindObjects.Click.......etc.
Me.txtStatus.BackColor = SystemColors.Window
Me.txtStatus.Text = ""
End Sub
So, functionally, if any of the specified form buttons are pressed, they would trigger the event that would first go to the above 'AnyButton_Click' Sub, then go do the actual button pressed routine (like 'reqAbout.Click').
In practice, most of the buttons do as described...when pressed, the do the 'AnyButton_Click' Sub, then go
on to do the buttons main function, but some buttons - and this is consistent and repeatable - first execute the button's main function THEN does the 'AnyButton_Click' sub.
First, is it obvious why this may not be working properly for some buttons and, Second, is there another (better?) way to achieve the same results?
Thanks for any help (or insight) provided,
Grant
-
Mar 25th, 2014, 10:14 PM
#2
Re: Multiple Button Press Detect Event Handler
When you're using the Handles clause there is no way of knowing in which order the event handlers will be called. You can either remove the Handles clause and instead use AddHandler in which case the events will be fired in the order you add them but it's probably easier to just add the two lines you have for the general click event handler to it's own method and call that method in each of the event handlers of the buttons.
-
Mar 25th, 2014, 10:23 PM
#3
Re: Multiple Button Press Detect Event Handler
I'm not sure what convention VB.NET uses regarding the order in which event handlers are registered and executed but I would expect that event handlers with a Handles clause are registered in the order they are encountered in code and executed in that same order. I just did some testing and that was not the case though. In the little testing I did, I couldn't see an obvious pattern. As a possible workaround, you could try using AddHandler and see if the event handlers are executed in the same order that they are registered. It means a bit more code though.
-
Mar 26th, 2014, 12:00 AM
#4
Re: Multiple Button Press Detect Event Handler
Originally Posted by jmcilhinney
I'm not sure what convention VB.NET uses regarding the order in which event handlers are registered and executed but I would expect that event handlers with a Handles clause are registered in the order they are encountered in code and executed in that same order. I just did some testing and that was not the case though. In the little testing I did, I couldn't see an obvious pattern. As a possible workaround, you could try using AddHandler and see if the event handlers are executed in the same order that they are registered. It means a bit more code though.
Based on my observation of decompiled code:
- Handles - Form Events are in the order encountered in code
- Handles - Added Controls Events are sorted by Method Name*
- AddHandler Events are in code order
* - Control Declarations are converted into Properties with the setter code containing the Addhandler/RemoveHandler code. Order appears to be based on some type sorting algorithm tied to method name. The control initializer code in (control = New ControlType) in IntializeComponent will trigger the setter code. If you manually add a control to the code (i.e. Private Name as New ControlType), the (control = New ControlType) is inserted into the contructor before IntializeComponent is called.
-
Mar 26th, 2014, 12:13 AM
#5
Re: Multiple Button Press Detect Event Handler
Is it a "good practise" to use two routines for the same event? At least that is what I understood from Eebigdogs post (using a "general click" routine and the spefic click routne for that event. I would assume that this is causing more problems then just the unknown order.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Mar 26th, 2014, 12:19 AM
#6
Registered User
Re: Multiple Button Press Detect Event Handler
hi i know this is off topic? but how can i create my own post? i just registered. and i need to find answers to my vb questions asap :P thank u in advance for the answers :P
-
Mar 26th, 2014, 05:32 AM
#7
Re: Multiple Button Press Detect Event Handler
Originally Posted by mimtinsay
hi i know this is off topic? but how can i create my own post? i just registered. and i need to find answers to my vb questions asap :P thank u in advance for the answers :P
In pretty much same way as you replied to this thread. Except that if you're not looking at a specific thread but at the list of threads inside a particular forum there's a big blue button at the top that reads + Post New Thread just like there inside a thread is a big blue button that reads + Reply to Thread
-
Mar 26th, 2014, 10:57 AM
#8
Thread Starter
Lively Member
Re: Multiple Button Press Detect Event Handler
Hi All,
Thank you for the interesting discussion.
I think, as Joacim suggested, my best
approach to this problem is to add a call,
from the button events, to the common
actions.
If I try the Addhandler method, and
position it ahead of the button events,
according to TnTinMN, this event would
trigger first, before the button events -
correct?
Many Thanks,
Grant
-
Mar 26th, 2014, 11:00 AM
#9
Re: Multiple Button Press Detect Event Handler
Trying to force the handler methods to fire in some specific pattern will lead to frustration. There is always a pattern, but it's hard to predict it well enough, and it could change (though not randomly), so it isn't reliable.
My usual boring signature: Nothing
-
Mar 26th, 2014, 04:18 PM
#10
Thread Starter
Lively Member
Re: Multiple Button Press Detect Event Handler
Thanks Shag....I'll "keep it simple" then.
Thanks all for the good advice.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|