Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unused C# code in Visual Studio

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?

like image 378
opt Avatar asked Oct 03 '14 22:10

opt


2 Answers

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.

like image 107
TyCobb Avatar answered Sep 24 '22 12:09

TyCobb


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.
like image 36
jigs Avatar answered Sep 23 '22 12:09

jigs