Results 1 to 10 of 10

Thread: Multiple Button Press Detect Event Handler

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2013
    Posts
    71

    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

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    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.

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Multiple Button Press Detect Event Handler

    Quote Originally Posted by jmcilhinney View Post
    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:

    1. Handles - Form Events are in the order encountered in code
    2. Handles - Added Controls Events are sorted by Method Name*
    3. 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.

  5. #5
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    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

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Multiple Button Press Detect Event Handler

    Quote Originally Posted by mimtinsay View Post
    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2013
    Posts
    71

    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

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2013
    Posts
    71

    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
  •  



Click Here to Expand Forum to Full Width