How to get event name in vb.net?

There are two handlers in a particular procedure, and then how to get which event handler executed.

eg

Private Sub TextBox1_Events(ByVal sender As System.Object, ByVal e As System.EventArgs)     Handles TextBox1.TextChanged, TextBox1.GotFocus

End Sub

how to get what event happened.

+3
source share
4 answers

Perhaps using StackTrace (maybe better, I'm not sure ...). Try the following code.

 Private Sub TextBox1_Events(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.GotFocus

        Dim s As New StackTrace(True)

        For Each f As StackFrame In s.GetFrames
            Debug.WriteLine(f.GetMethod.Name)
        Next

    End Sub

When the text field receives focus, the following is recorded:

TextBox1_Events

OnGotFocus

OnGotFocus

Wmsetfocus

Ect .......

Where when its text changed an event

TextBox1_Events

OnTextChanged

OnTextChanged

Ect ....

I'm sure you could write something using this to do what you need. But I totally agree with the other handler guys.

+2
source

In this case you cannot.

  • , sender
  • e , EventArgs ( ), , EventArgs,

, , - , , .

, . , EventArgs ( ), .

.

+2

It's impossible. If you are in a situation where you need to know what event has occurred, you will always be better off using two separate handlers.

+1
source

Since you are dealing with 2 events (similar in signature) emitted by the same control, the easiest / cleanest way to resolve this issue would be to have two separate event handlers (as Merlin Morgan-Graham suggested):

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)     Handles TextBox1.TextChanged
        'the TextChanged specific code would go here
        HandletTextBox1EventInternal(sender, e)
    End Sub 

    Private Sub TextBox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)     Handles TextBox1.GotFocus
        'the GotFocus specific code would go here
        HandletTextBox1EventInternal(sender, e)
    End Sub

    Private Sub HandleTextBox1EventInternal(ByVal sender As System.Object, ByVal e As System.EventArgs)
            'code common to GotFocus and TextChanged handlers
    End sub
+1
source

All Articles