Is there a way in VB.NET to replicate C# behavior on code snippets in case of capturing events?
I.E. in C#:
Btn.Click+=
and then double-tab. Creates an handler for btn.click event.
Is there a way in VB.NET to replicate C# behavior on code snippets in case of capturing events?
I.E. in C#:
Btn.Click+=
and then double-tab. Creates an handler for btn.click event.
In C# you can only connect events explicitly, using +=
. The VB equivalent is AddHandler
.
But VB also lets you connect events implicitly using the Handles
keyword. Notice that VB event handling is more flexible than C#.
There are several ways to automatically create event handler code using Handles
.
In design-view, just double-click the button.
Or, in design-view again:
a. In the button's properties window, click the "lightning bolt" to view events.
b. Double-click the Click
event.
Or, in code view, you can use the two drop-downs at the top of the window
a. Select the button in the left-hand one.
b. Select the Click
event in the right-hand one.
EDIT: in VB.Net 2010 you can handle events with statement lambdas. Example:
AddHandler b.Click, _
Sub(sender As Object, e As EventArgs)
MsgBox("Button Clicked")
'insert more complex logic here
End Sub
OK, so maybe you know about implicit event handling and anonymous methods, and you've still decided you really need to wire up explicit local methods from AddHandler
all the time. Maybe there's a third-party add-in that supports it. Maybe CodeRush does, I don't know (if anyone knows please leave a comment). Or you could even create your own add-in.
Strangely enough the VB.NET team didn't implement such feature in Visual Studio.