Home > .Net CLR, WPF > Add an Event Handler VB VS C#

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

Advertisements
Categories: .Net CLR, WPF
  1. December 3, 2010 at 11:28 am

    You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

  2. February 1, 2011 at 12:11 pm

    It’s laborious to find educated individuals on this topic, but you sound like you recognize what you’re talking about! Thanks

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: