2

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.

Daniel Wolfe
  • 662
  • 1
  • 8
  • 19
  • 4
    VS doesn't arbitrarily create stubs. Are you double-clicking on a control before this happens? – Michael Todd May 04 '12 at 16:12
  • 1
    If you are using the forms designer, it will create these automatically if you (double?) click on the buttons in the designer. – yoozer8 May 04 '12 at 16:12
  • 1
    First of all, 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. It's not important anyway, because I'm not clicking on the control. – Daniel Wolfe May 04 '12 at 16:16
  • That's not necessarily true. You might be defining them during runtime (i.e. there's not an event initially set for the control but there's a definition _in another file_ that is set up during runtime) which would cause the situation you're seeing. Again, VS doesn't create stubs without some user involvement. – Michael Todd May 04 '12 at 16:18
  • 1
    First click to select the grid, second click to select the column header - et voilà - you have the double click! – Olivier Jacot-Descombes May 04 '12 at 16:19
  • 3
    Using partial classes to compartmentalize UI code does confuse Visual Studio. The common method to break up complex UI is to instead compose your form of smaller UserControls. This also provides a more OOP style solution. – IngisKahn May 04 '12 at 16:20
  • I should have stated when I first posted the question: it's creating FOUR separate stubs for four previously defined event handlers. When you double-click on a control (which I'm not) it only creates one. – Daniel Wolfe May 04 '12 at 16:32

3 Answers3

4

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.
WiredEarp
  • 383
  • 3
  • 11
2

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.

John Arlen
  • 6,539
  • 2
  • 33
  • 42
  • I like the answer, but I'm not defining these event handlers manually; the event handlers are set up in xxx.Designer.cs. In fact, when I double click on the control, it takes me to a method that I already have defined: dataGridView1_CellContentClick. – Daniel Wolfe May 04 '12 at 16:30
1

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.

Community
  • 1
  • 1
Daniel Wolfe
  • 662
  • 1
  • 8
  • 19
  • This unfortunately didn't resolve anything for me, new events were still created in the wrong class, and the invalid handlers kept being created. I've posted a working solution above. – WiredEarp Sep 24 '15 at 05:54
  • I don't think so... "Clean" command deletes objects, debug informations and binaries files inside your "bin" directory. It shouldn't touch the source code. – tedebus Feb 10 '17 at 15:13