Roland Weigelt

Born to Code

News

  • Twitter: English | Deutsch
  • Meet me:
    .NET Open Space vom 17.10. bis 18.10.2009 in Leipzig
  • GhostDoc:
  • .NET User Group:
  • Personal homepage:
  • XING:
    XING

.NET related Links

How to Delete an WinForms Control Event Handler Quickly

Recently a long-time user of Visual Studio looked over my shoulder when I deleted an event handler from my source code, and he said “uh, wait… how did you do that?”. If you are like him, not using the keyboard for this task, and would like to speed up things a bit, you may find this tip useful.

Imagine you have an event handler in a file MyForm.cs, e.g.

private void myButton_Click( object sender, EventArgs e )
{
   // ... some code ...
}

Deleting only this method obviously results in a compiler error as there’s a line

this.myButton.Click += new System.EventHandler( this.myButton_Click );

in the file MyForm.Designer.cs that has to be deleted as well.

To delete the event handler properly in Visual Studio 2005, using only the keyboard, perform the following steps:

  • In the MyForm.cs file, move the cursor on the name of the method file (i.e. myButton_Click) and press Shift-F12 (command Edit.FindAllReferences), which will bring you to the Find Symbols Results window.
  • press the Cursor Down key to move to the second line showing the occurrence of myButton_Click in the designer-generated code that connects the event with the handler method*
  • press Enter to jump to the MyForm.Designer.cs file
  • delete the current line by pressing Ctrl-Shift-L (command Edit.LineDelete)
  • jump back to the MyForm.cs by pressing Ctrl-Tab
  • and finally delete the code of the event handler method.

The description of the steps may sound a bit complicated at first, but you’ll find that the actual keystrokes come naturally pretty soon.

_____________
*) Note that this is only the case when the form was actually created in Visual Studio 2005 – keep your eyes open when working with forms in older projects that were started in Visual Studio .NET 200x.

Comments

Fabse said:

the normal way of doing this:

1. delete the handler.

2. press F5

3. doubleclick the error

4. delete the line

# December 18, 2006 12:59 AM

overthetop said:

delete the handler, go to designer, serach and destroy ;)

# December 18, 2006 7:40 AM

gorshing said:

I also use Ctrl+- for navigating backwards.

I believe it is View.NavigateBackward and View.NavigateForward.

# December 27, 2006 4:28 PM

WeigeltRo said:

Fabse: Running the compiler is exactly what you want to avoid in projects of a certain size ;-)

Gorshing: Yes, Ctrl+- can be used instead of the Ctrl-Tab in the penultimate step.

# January 4, 2007 3:39 AM

dotob said:

dont know if i understand overthetop right, but it seems like my preferred way:

- delete the handler

- goto to designer, when he recognizes the handler is missing he will automagically delete the *+=* line

- done

# January 4, 2007 10:49 AM

Sean Kearon said:

Or you could (should!!) load up Resharper (http://www.jetbrains.com/resharper/) and hit Alt + Del.  

Alternatively, if you have the VS 'track current file option' enabled and Resharper, delete the handler, hit Ctrl + Alt + L (to open the Solution Explorer), hit down arrow and then Enter to open the designer.cs file, then hit F12 for Resharper to take you to the line where the delegate is assigned.

# February 7, 2007 11:45 AM