6

when working on a windows form I have accidently clicked on buttons and now I have part of code related to this click event. I don't need them and I would like to remove these parts from the code but, if I do, Visual Studio complains when compiling cause it search for that missing code. How can I get rid of unused click events on my code?

Nathan A
  • 9,863
  • 4
  • 42
  • 57
opt
  • 467
  • 9
  • 22
  • 2
    When Visual Studio gives this error, is it referring to a specific line of code (likely in the designer file)? Go to that line of code and remove the reference to the deleted event handler. – David Oct 03 '14 at 22:38
  • 6
    If you don't just want to delete the statement, the simple way, then go back to the Properties window, click the lightning bolt icon, right-click the event and select Reset. – Hans Passant Oct 03 '14 at 22:39
  • Thanks, as suggested I only had to remove also the reference to the deleted event handler. Now it seems to work fine. Thanks guys. – opt Oct 03 '14 at 22:54
  • Fyi - you can remove it by clicking in the column for the event in the properties windows and pressing Delete. This only works if its empty iirc. – Simon Whitehead Oct 03 '14 at 22:59
  • Possible duplicate of [Winforms - Visually remove button click event](https://stackoverflow.com/questions/2751782/winforms-visually-remove-button-click-event) – StayOnTarget Jan 29 '18 at 16:47

2 Answers2

9

When you double-click on a control, the default event gets wired up and a stubbed out handler gets created for you.

The stubbed handler you know about as you saw it and deleted.

private void button1_Click(object sender, EventArgs e)
{
}

The other piece is where the event actually gets wired. This is where the compile error comes from. You deleted the event handler, but did not remove the event subscription.

This can be found in the Designer.cs file attached to the specific form.

private void InitializeComponent()
{
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Name = "button1";

    //This is the line that has the compile error.
    this.button1.Click += new System.EventHandler(this.button1_Click);
}

As mentioned in the comments, you can go to the event properties for that control and reset the event, but you can also go into the designer and remove the invalid line. Using the Reset command will remove the stub and the event subscription.

TyCobb
  • 8,411
  • 1
  • 27
  • 47
0

Here is another solution:

  1. Go to property (press F4), then go to an event.
  2. Select unwanted event, then right-click on event icon.
  3. Click 'Reset' to remove it from designer.cs automatically.
  4. Remove it in .cs file manually.

Thank You.

brombeer
  • 6,294
  • 4
  • 18
  • 26
jigs
  • 1
  • 1
  • 2