Answered by:
vb.net get event name

Question
-
Hi Experts,
I want to know a better way to get event name inside sub txt_TextCount_event.
Dim mb As Reflection.MethodBase = Reflection.MethodBase.GetCurrentMethod Debug.Print("mb.name " & mb.Name) it always give me mb.name : txt_TextCount_Validating, not txt_TextCount_Validated
Thank you very much !
Francis SZE
Private Sub txt_TextCount_event(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_TextCount.Validating, txt_TextCount.Validated Dim errorMsg As String = String.Empty Dim mb As Reflection.MethodBase = Reflection.MethodBase.GetCurrentMethod Debug.Print("mb.name " & mb.Name) If mb.Name.Contains("Validating") Then do something ElseIf mb.Name.Contains("Validated") Then do the other some thing End If End Sub
Monday, June 25, 2012 7:26 AM
Answers
-
Seems to me an impossible way,
An event is handled by a method, in VB it is an addition in the Code made my Microsoft that you can tell which events are handled direct to the description of the method.
However, that is not the case in C#, C++ or F# there you have always to do the equivalent of this VB code
AddHandler TheControl.TheEvent, Addressof TheMethodToBeHandled
Be also aware that an method can handle an endless quantity of events, maybe that explains it easier why you cannot get the event of a method.
Success
Cor- Proposed as answer by Armin Zingler Monday, June 25, 2012 11:42 AM
- Unproposed as answer by fsze88ATmeDOTcom Monday, June 25, 2012 12:10 PM
- Marked as answer by fsze88ATmeDOTcom Tuesday, June 26, 2012 2:45 AM
Monday, June 25, 2012 8:45 AM -
In addition to Cor:
It doesn't make sense to check the name of the current method as it never changes at runtime. You specify it in code and it's always "txt_TextCount_event". (BTW it's never "txt_TextCount_Validating"). EDIT: Names shouldn't matter at runtime anyway as they are made for the programmer only. At runtime, there are pointers and offsets.
The solution to be able to distinguish between events is not using the same event handler.
Armin
- Edited by Armin Zingler Monday, June 25, 2012 11:51 AM
- Marked as answer by fsze88ATmeDOTcom Tuesday, June 26, 2012 2:45 AM
Monday, June 25, 2012 11:50 AM
All replies
-
Seems to me an impossible way,
An event is handled by a method, in VB it is an addition in the Code made my Microsoft that you can tell which events are handled direct to the description of the method.
However, that is not the case in C#, C++ or F# there you have always to do the equivalent of this VB code
AddHandler TheControl.TheEvent, Addressof TheMethodToBeHandled
Be also aware that an method can handle an endless quantity of events, maybe that explains it easier why you cannot get the event of a method.
Success
Cor- Proposed as answer by Armin Zingler Monday, June 25, 2012 11:42 AM
- Unproposed as answer by fsze88ATmeDOTcom Monday, June 25, 2012 12:10 PM
- Marked as answer by fsze88ATmeDOTcom Tuesday, June 26, 2012 2:45 AM
Monday, June 25, 2012 8:45 AM -
In addition to Cor:
It doesn't make sense to check the name of the current method as it never changes at runtime. You specify it in code and it's always "txt_TextCount_event". (BTW it's never "txt_TextCount_Validating"). EDIT: Names shouldn't matter at runtime anyway as they are made for the programmer only. At runtime, there are pointers and offsets.
The solution to be able to distinguish between events is not using the same event handler.
Armin
- Edited by Armin Zingler Monday, June 25, 2012 11:51 AM
- Marked as answer by fsze88ATmeDOTcom Tuesday, June 26, 2012 2:45 AM
Monday, June 25, 2012 11:50 AM -
Hi experts,
I want to call same sub on events Validating and Validated.
As I building a large project, I want the coding as short as possible and easy to follow.
Any other suggest could be short and easy to follow?
Thank you very much
Best Regards
Francis SZE
Private Sub txt_TextCount_event(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_TextCount.Validating, txt_TextCount.Validated If mb.Name.Contains("Validating") Then do something ElseIf mb.Name.Contains("Validated") Then do the other some thing End If End Sub
Monday, June 25, 2012 12:21 PM -
Again: The solution to be able to distinguish between events is not using the same event handler.
You want it simple but contradict yourself by writing code trying to distinguish between the events instead of simply using different handlers. Like we all do BTW.
It's like dripping two drops of water into the same bucket of water and finding them afterwards. Instead use different buckets.
Armin
- Edited by Armin Zingler Monday, June 25, 2012 12:36 PM
Monday, June 25, 2012 12:36 PM