1_RA
- hrafnulf13
- Sep 26, 2020
- 1 min read
Updated: Oct 13, 2020
Event Handlers
I prefer C# over VB.net, since I never had experience with it. While, C# is quite similiar to languages that I tried before (C, C++, Java).
During the application exercies we had an opportunity to see the differences in event handling between these two languages.
C#
In C#, classes manage event handlers (i.e. DragDrop, DragEnter, MouseLeave, MouseEnter, Click) by subscribing to that events via:
<some class>.Event += new <some event handler>()
It also might require to add an callback function when subscribing to a certain event, such that:
<some class>.Event += new <some event handler>(< some callback function>)
Check Form1.Designer.cs, for example:
this.button1.Click += new System.EventHandler(this.button1_Click)
Callback functions are functions that are invoked when a certain event occurs. Thus, when button1 is clicked it will call the button1_Click function.
Check the references [1, 2] for more detailed overview.


VB.net
In VB.net, event handling can be done in two ways: via AddHandler or Handler/WithEvents [3]. In the first case, it is quite similar to C#:
AddHandler <some class>.Event, AddressOf <some callback function>
Then define callback function.
In the second case, it is done during the definition of the callback function (which can be seen in Form1.vb). So it's quite compact.
Sub <Callback function name>(...) Handles <some class>.Event
---some code---
End Sub
Check Form1.vb, for example:
Private Sub Button1_Click(...) Handles Button1.Click
---some code---
End Sub
Check the references [1-3] for more detailed overview.



References
https://sites.harding.edu/fmccown/vbnet_csharp_comparison.html#events
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/how-to-call-an-event-handler
Comments