1

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.

Bobby
  • 11,419
  • 5
  • 44
  • 69
Faruz
  • 9,909
  • 10
  • 48
  • 66
  • may sound stupid but often it works for events documented online: google it and copy/paste handler declaration from examples. I know, this doesn't answer the question, but is better than hovering the event member to hardly read its tooltip, and becomes an easy workaround for things like `dte.Events.CommandEvents.BeforeExecute` – Ivan Ferrer Villa Nov 05 '15 at 03:34

2 Answers2

5

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.

  1. In design-view, just double-click the button.

  2. 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.

  3. 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.

Community
  • 1
  • 1
MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • I'll choose from the list. It seems like I forgot all my horrible VB6 skills... I'm too spoiled now. – Faruz Nov 15 '10 at 14:03
3

Strangely enough the VB.NET team didn't implement such feature in Visual Studio.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @Frauz, I understand your frustration :-) – Darin Dimitrov Nov 15 '10 at 07:41
  • @Faruz, @Darin. There's no shortcut for explicitly connecting events. But in VB it's more common to [connect events implicitly](http://stackoverflow.com/questions/2547033/what-is-the-difference-between-events-in-vb-and-c/2547083#2547083). Which is probably why there are no shortcuts for the explicit method. [My answer lists some shortcuts](http://stackoverflow.com/questions/4182177/code-snippets-shortkey-in-vb-net/4183940#4183940) for the implicit method. – MarkJ Nov 15 '10 at 11:42
  • @Faruz, @Darin. The better you understand *any* language, the less frustrating it will be. There are only a [few differences](http://visualstudiomagazine.com/columns/article.aspx?editorialsid=2894) between C# and VB, and they are [easily learnt](http://stackoverflow.com/q/1337253/15639). – MarkJ Nov 15 '10 at 11:43
  • @MarkJ, the implicit way of handling events in VB.NET can be applied only to some global variables. What if you had a local variable and you wanted to apply an event handler. You would need to do it explicitly, by declaring the method body first and then wiring it up while in C# it's just a matter of double Tab. I am not an expert in VB.NET, I just have some general culture but personally prefer C# :-) Don't want to start flame wars though. – Darin Dimitrov Nov 15 '10 at 12:23
  • @Darin Implicit event handling can be applied to module-level variables as well as globals: the classic examples are controls created by drawing in the forms designer. I think it might be uncommon to want to wire a local variable's event to a new explicit method. When you have a local variable, typically isn't it better to create a lambda? If local variable->explicit method really is needed frequently, it might be worth checking whether CodeRush supports it. – MarkJ Nov 15 '10 at 12:58
  • @MarkJ, indeed you are correct. Local variables work better with anonymous functions. – Darin Dimitrov Nov 15 '10 at 13:07