How to Generate Event Handlers in VB.NET

I want to quickly generate an event handler for a control on my form while in the code view of Visual Studio, preferably just using keyboard shortcuts (instead of having to use Visual Studio's combobox selectors at the top.  I'm writing in VB.NET, 2.0 framework, R# 4.5.

I can't find any documentation or forum discussions on how to do this, just that R# can do this.  Supposedly in C# you can generate one by hitting the Tab key twice after writing "new EventHandler(" or something like that, but I can't seem to get anything like that to work in VB.NET (obviously taking syntax into account).  Does anyone know how to do this?

Here's what I want it to write for me, for example:


Private Sub SaveToolStripButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveToolStripButton.Click

End Sub

Thanks in advance.

2 comments

Hi,

In C# you can do it in few different ways. Supposing we have next code:

{code}
using System;
using System.Collections;
using System.Windows.Forms;


namespace X
{
  public class Foo
  {
    public void Bar(ToolStripButton button)
    {
      button.Click +=  {caret here}
    }
  }
}
{code}

1. You can invoke smart completion here and it suggests you to create a method (ctr-shift-space or alt-ctr-space)
2. You can write your method name and invoke quick fix to create appropriate method (alt-enter or clicking on the red bulb)

In VB.NET :

{code}
Imports System.Windows.Forms

Public Class App
  Public Sub Foo(button As ToolStripButton)
    AddHandler button.Click , {caret here}
  End Sub
End Class
{code}

1. You can invoke smart completion here and it suggests you to create anonymous function (creation of a method in this situation is not supported yet but we plan to fix it)
2. You can write 'addressof MethodName' and invoke quick fix to create appropriate method

Enjoy! =)

Regards,
Olga

0

Thanks, Olga.  The Alt + Space command to then generate the method works after I've written out the entire line.  I would prefer to use something similar to your example of Ctrl + Shift + Space / Ctrl + Alt + Space but whenever I try that, R# says it has no suggestions.

AddHandler MyButton.Click, {carat}

I've only tried this on my office PC, where I'm running VS 2005 and R# 4.5.1 and coding in VB.  I will try it on my home PC as well where I'm running VS 2008, R# 4.5.2, and C# and will see if it works there.

Also, it seems like for VB, at least in VS 2005, you can't actually hide the navigation bar (the class/member aka type/member comboboxes at the top of the editor window) even though you can toggle a checkbox to supposedly turn it off in the options--it just doesn't actually do it.  I know this works for C#.  So what I'm doing instead is using a keyboard shortcut that I found on Sara Ford's blog to set focus into the first combobox without having to pick up my mouse: Ctrl + F2 (http://blogs.msdn.com/saraford/archive/2007/12/05/did-you-know-how-to-reach-the-navigation-bar-via-the-keyboard.aspx).

Thanks again.

0

Please sign in to leave a comment.