Add an Event Handler VB VS C#
If you constantly switch between C# and VB because of working with different team, you would find some VB codes confusing in compare to C#. For me the syntax of event handling is a common case to forget in VB.
The following example is from MSDN web site for handling a Routed Event in WPF:
C# Source code:
public partial class RoutedEventAddRemoveHandler {
void MakeButton(object sender, RoutedEventArgs e)
{
Button b2 = new Button();
b2.Content = "New Button";
// Associate event handler to the button. You can remove the event
// handler using "-=" syntax rather than "+=".
<strong>b2.Click += new RoutedEventHandler(Onb2Click);
</strong> root.Children.Insert(root.Children.Count, b2);
DockPanel.SetDock(b2, Dock.Top);
text1.Text = "Now click the second button...";
b1.IsEnabled = false;
}
void Onb2Click(object sender, RoutedEventArgs e)
{
text1.Text = "New Button (b2) Was Clicked!!";
}
VB Source Code:
Public Partial Class RoutedEventAddRemoveHandler Public Sub MakeButton(sender As Object, e As RoutedEventArgs) Dim b2 As Button = New Button() b2.Content = "New Button" <strong>AddHandler b2.Click, AddressOf Onb2Click</strong> root.Children.Insert(root.Children.Count, b2) DockPanel.SetDock(b2, Dock.Top) text1.Text = "Now click the second button..." b1.IsEnabled = False End Sub Public Sub Onb2Click(sender As Object, e As RoutedEventArgs) text1.Text = "New Button (b2) Was Clicked!!" End Sub
Ref: http://msdn2.microsoft.com/en-us/library/ms743596(VS.85).aspx