I am Using two types of event-handling Enter
and Leave
in one Function, using with If-Statement
.
Anybody, please, correct my script.
Private Sub MaskedTextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrgNameTextBox.Enter, OrgNameTextBox.Leave
If Event = Enter Then
CType(sender, Control).BackColor = Color.Yellow
CType(sender, Control).ForeColor = Color.Black
ElseIf Event = Leave Then
CType(sender, Control).BackColor = Color.Black
CType(sender, Control).ForeColor = Color.White
End If
End Sub
Ideally, you should have two separate handlers for two separate events because you are doing different things on different events. But if you must have only one handler then you can try something like, and I am pretty sure this is a HACK:
Private Sub MaskedTextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrgNameTextBox.Enter, OrgNameTextBox.Leave
Dim stackTrace As New StackTrace(True)
Dim frame As StackFrame = stackTrace.GetFrames[0]
If(frame.GetMethod.Name = 'OnEntered') <CHECK THE EVENT NAME, THIS IS JUST AN EXAMPLE>
CType(sender, Control).BackColor = Color.Yellow
CType(sender, Control).ForeColor = Color.Black
End If
If(frame.GetMethod.Name = 'OnLeave')
CType(sender, Control).BackColor = Color.Black
CType(sender, Control).ForeColor = Color.White
End If
End Sub
I had seen the way to get the event name somewhere in StackOverflow only, I will update the answer once I find it to give the correct credit :)
EDITED ANSWER
Private Sub MaskedTextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrgNameTextBox.Enter, OrgNameTextBox.Leave
Dim stackTrace As New StackTrace(True)
For Each frame As StackFrame In s.GetFrames
IF(frame.GetMethod.Name = 'OnEntered') <CHECK THE EVENT NAME, THIS IS JUST AN EXAMPLE>
CType(sender, Control).BackColor = Color.Yellow
CType(sender, Control).ForeColor = Color.Black
End IF
IF(frame.GetMethod.Name = 'OnLeave')
CType(sender, Control).BackColor = Color.Black
CType(sender, Control).ForeColor = Color.White
End If
NEXT --THIS NEEDS TO BE CHECKED, YOU MAY WANT TO EXIT BEFORE,ONCE FOUND YOUR EVENT.
End Sub
Also found the actual answer mentioned earlier for credit. It's How to get event name in vb.net?