How to delete a component from a project (C# with .NETFramework4.7.2)

Hobby02 26 Reputation points
2021-03-26T14:36:46.267+00:00

A question of a beginner about Visual Studio Community 2019 I created a project by clicking at "Create a new Project" with Language: C#, Platform: Windows, Project: Type Desktop and Template: Windows Forms App (.NET Framework) I placed a component, for example a button, from the toolbox on my Form1. By accident I double-clicked too early on that button ,but the code ("private void button1_Click(object sender, EventArgs e)" was already generated in Form1.cs. I had firstly to modify the name "button1" (in button1_Click(object sender, EventArgs e) into a, for me, more usable name (e.g. btnTest). I tried to repair my mistake by clicking in the Form (Form1.cs[Design]) on that button1 and changed the name in the property window. Going back to the code in Form1.cs I saw that nothing was changed. Deleting the button out of my Form1.cs[Design] (right-click Delete) did not remove the belonging complete code out of Form1.cs Then I created a new project and placed again a button1 out of the Toolbox into the Form1 and double-clicked intentionally on the button1 to let create the code. Now I renamed in Form1.cs with the editor (via Edit-Rename) "private void button1_Click(object sender, EventArgs e)" into "private void btnTest_Click(object sender, EventArgs e)" but with Error messages as a result. My question is simple in fact. How can I remove (delete) on the right way a component from my project? An additional question popped up during these experiments. And that is that the components in the Toolbox are not always shown after launching Visual Studio 2019 with (one of) the above created Projects after selecting the Form1.cs[Design] tab and selecting View-Toolbox (Ctrl + Alt + X) Thank you for your help in advance.

Developer technologies C#
0 comments No comments
{count} vote

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2021-03-26T15:57:30.92+00:00

    You have a large wall of text that is hard to follow. I'm not sure if you're trying to get rid of the method you accidentally created when you double clicked the control in the designer, rename the method/component, or remove the component entirely from the form. So let's answer them all.

    You cannot simply delete the method in the codebehind because when you go back to the designer it'll crash with the YSOD. To do it "properly":

    1. Go to the designer
    2. Select the control that is associated with the event
    3. Go to the Properties window
    4. Switch to the Events view (second group of buttons in the window's toolbar, lightning bolt)
    5. Find the event associated with the method and then delete the method name from the event
    6. If the method is empty and not used elsewhere then generally it'll get auto-deleted from the code
    7. If it is not auto-deleted nor used anywhere else (use the CodeLens reference count above the method name) then delete the method

    If you are getting the YSOD then the error message at the top tells you want is wrong. On the right side is an option like "Go to Code" click that to jump into the designer.cs and fix the compiler error. Close the designer.cs file and the designer UI should refresh.

    If you want to rename an event handler method, or really any identifier that might be in use, then symbolically rename it.

    1. Right click the identifer and select Rename
    2. Start typing the new name in the editor and the IDE will reflect the change
    3. Once done click the Apply button in the Rename dialog that popped up to symbolically rename the identifier everywhere it was being used.

    To remove a component (or control) from the form entirely.

    1. Select the control in the designer and press Delete (or use the context menu)
    2. You will have to manually clean up any event handlers in the code behind that were associated with the component (again use CodeLens to see this)
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Hobby02 26 Reputation points
    2021-03-27T21:11:41.32+00:00

    @Michael Taylor

    Thank you for your quick and extended help.
    My apologies for the late reply and the bad formatting of my previous message which was written first in a text editor followed by a copy and paste.

    It has completely succeeded to remove the code of an unwanted method/component following your paragraphs 1...6.
    After that I deleted the concerning component in the Designer.
    The instruction mentioned in Paragraph 7 was not necessary in this case.

    Renaming an event handler was ok exactly on the way you have described.
    However after clicking on the concerned component in the Designer window it showed in the Property Window (after clicking on the lightning bolt) the event name correct but on top of that window's toolbar the previous eventname still existed.
    I went back to the Form1.cs code and right clicked on the renamed event and selected Find All References.
    And found in the Form1.Designer.cs file still the previous name in lines beginning with "this."
    So I manually renamed these name as well. Then the event name on top of the property window is OK now.
    Apparently the first Rename action was not sufficient (or may be I have done something wrong).

    Removing a component from the Form with the Delete button was also ok and no YSOD cappeared after I had manually cleaned up the corresponding event handler in the code

    Anyway you has helped me further.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.