简体   繁体   中英

Visual Studio keeps creating event handlers that are already defined

I have a Windows Form that have defined in multiple partial-class files. In one of these files (xxx.Query.cs), I have all the event handlers defined for a DataGridView. However, occasionally, Visual Studio decides that it wants to create stubs for four of these event handlers in the main file for the form (xxx.cs). I end up getting four errors which say something along the lines of "Type 'xxx' already defines a member called 'dataGridView1_ColumnHeaderMouseClick' with the same parameter types".

How can I get Visual Studio to quit trying to create stubs for these? It's most vexing.

I experienced this problem today after splitting a large (legacy) form into a partial class while adding a new section to it. Despite this being an old question, i'm posting as many of the answers to the original question are incorrect. Specifically, many of them claim there is no way that Visual Studio can be generating these without double clicking. I'd like to confirm that VS does do this in certain circumstances.

In my particular situation, I added a new tab to a tab control, and put the new tabs events and code in the new class. Originally, I had placed this code (including events) at the end of the main form, which may have a bearing on the issue. After moving the code, if I add a control to any part of the main form (a separate tab, for example), VS generates empty event handlers for the 3 events I have in my new partial class, despite having full event handlers in this class.

The only solution I have for now is deleting the empty handlers from the bottom of my main form code when the problem occurs. Solution cleaning will not delete these blank handlers.

Since all 3 generate consistently each time, and since they are on a tab which is hidden well behind the main form when I add a test control (so no chance of double clicking, certainly no chance of doubleclicking all 3), I can say with confidence that this IS a Visual Studio bug (VS 2013 in my case).

UPDATE 24/9/15: After getting sick of deleting handlers, I did some more searching, and found a solution to the issue. Heres the text of the mustiy's solution from MS forums ( https://social.msdn.microsoft.com/Forums/windows/en-US/79910eaa-84e7-472d-95ee-4b8ddfbf99f0/multiple-partial-class-files-cause-problems-with-forms-designer?forum=winforms&prof=required ). This fix works for me, although click creating events creates them in the wrong class still, but after moving them, they work correctly. It also prevents the invalid event handlers from being created.

i had same problem and came accross your post, but i wasn't sattisfied. It had to be a solution for this because VS IDE does it.
Here is the solution :
Open the .csproj project file with notepad and let's say for a Form1.cs file you will see a configuration like
<Compile Include="Form1.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
  <DependentUpon>Form1.cs</DependentUpon>
</Compile>
If you create a new file called Form1Events.cs and put another partial class of Form1 and move the events to the new partial class. In the new .csproj file you the new file like this below
<Compile Include="Form1Events.cs">
  <SubType>Form</SubType>
</Compile>
Something is missing you have to tell the VS IDE that this Form1Events.cs file is dependent to Form1.cs. As the IDE do this for Form1.Designer.cs file make the same with Form1Events.cs and put a "DependentUpon" tag into Form1Events.cs. Last the .csproj file look like this below
<Compile Include="Form1.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
  <DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Form1Events.cs">
  <DependentUpon>Form1.cs</DependentUpon>
  <SubType>Form</SubType>
</Compile>
And if you save the .csproj file the IDE reloads the project and on the Solution exlorer tree you see that the new partial class is put below Form1.cs file. And if you open the designer and double click a previous decalred and moved event it will navigate to the new file.

In a comment, you claim: "if I were double clicking on a control that already has an event handler defined, it will take me to that event handler, not create a new one."

This is not true if you are hooking the events manually.

public Form1() {
    InitializeComponent();
    myButton.Click += new EventHandler(myButton_Click);
}

void myButton_Click(...) { }

If you go to the Designer and either double-click the control or assign the event in the Property Grid (and you'll note it's not identified in the property grid), Visual Studio will create a completely additional entry in InitializeComponent() and the main form code file:

void myButton_Click1(...) { }

Visual Studio will never automatically create control event handlers without your involvement in the designer somehow. And unfortunately it is VERY easy to cause VS Designer to think you double-clicked - most easily during that oft-long pause when first loading the form.

Apparently, what needs to be done is to clean the solution. This, apparently, erases all the serialization that's done behind the scenes. (I'm no expert in how Visual Studio does its serialization, so I could have the terminology wrong.)

Visual Studio (2008) 'Clean Solution' Option

See, my momma don't raise no dummies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM