none
How to check if event an was handled, from a different event procedure?

    Question

  • How can I check if an event was handled from a different event procedure?

    e.g.

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged
    
    End Sub
    
    Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs) Handles ToolStripComboBox1.DropDownClosed, ToolStripComboBox1.SelectedIndexChanged
    
    IF ComboBox1_SelectedIndexChanged ? THEN ....
    
    End Sub

    Friday, March 17, 2017 2:15 PM

All replies

  • How can I check if an event was handled from a different event procedure?

    e.g.

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged
    
    End Sub
    
    Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs) Handles ToolStripComboBox1.DropDownClosed, ToolStripComboBox1.SelectedIndexChanged
    
    IF ComboBox1_SelectedIndexChanged ? THEN ....
    
    End Sub


    The consumer (your form's class) can subscribe to all of them and all of them will respond, so it's not an either/or situation at all.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, March 17, 2017 2:29 PM
  • To show what I mean, try this if you care to:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) Handles MyBase.Load
    
            ' Initiliaztion here, if applicable...
    
        End Sub
    
        Private Sub SubA(sender As System.Object, _
                         e As System.EventArgs) _
                         Handles TextBox1.TextChanged
    
            MessageBox.Show("SubA")
    
        End Sub
    
        Private Sub SubB(sender As System.Object, _
                        e As System.EventArgs) _
                        Handles TextBox1.TextChanged
    
            MessageBox.Show("SubB")
    
        End Sub
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, March 17, 2017 2:32 PM
  • What is it you want to do exactly?

        Private FirstIndex As Integer
    
        Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox1.DropDownClosed
            If Not FirstIndex = ComboBox1.SelectedIndex Then
                MsgBox(ComboBox1.SelectedIndex.ToString)
            End If
        End Sub
    
        Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown
            FirstIndex = ComboBox1.SelectedIndex
    
        End Sub
    

    Friday, March 17, 2017 2:42 PM
  • It would be unusual to have one handler respond to two different events like that, and I think that Tommy has likely shown the alternative for what you are trying to do - first record the current selection when the dropdown opens, then compare the current selection to the recorded selection when the drop down closes; if there is a difference, execute your code.

    That said, you can figure out which method lead to the event handler executing by examining the stack trace within the method:

        Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs) Handles ToolStripComboBox1.DropDownClosed, ToolStripComboBox1.SelectedIndexChanged
            Dim trace As New StackTrace
            'frame(0) = this method
            'frame(1) = raise event call
            'frame(2) = OnEventMethod which called raise event
            Select Case trace.GetFrame(2).GetMethod.Name
                Case "OnDropDownClosed"
                    'user opened and closed dropdown without making a change
                    Label1.Text = "Drop Down Closed"
                Case "OnSelectedIndexChanged"
                    'user opened dropdown and selected an item (could have been
                    'the currently selected item - does not mean the selection actually changed
                    Label1.Text = "Selected Index Changed"
            End Select
        End Sub

    I suspect you want to use code similar to what Tommy has shown.  This example is provided for completeness.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 17, 2017 3:31 PM
    Moderator