Visual Studio 不断创建已定义的事件处理程序

本文关键字:定义 事件处理 程序 创建 Studio Visual | 更新日期: 2023-09-27 18:33:07

我有一个在多个分部类文件中定义的Windows窗体。在其中一个文件 (xxx.Query.cs( 中,我为 DataGridView 定义了所有事件处理程序。但是,有时,Visual Studio 决定要在窗体 (xxx.cs( 的主文件中为其中四个事件处理程序创建存根。我最终得到四个错误,这些错误类似于"类型'xxx'已经定义了具有相同参数类型的名为'dataGridView1_ColumnHeaderMouseClick'的成员"。

如何让 Visual Studio 停止尝试为这些创建存根?这是最令人烦恼的。

Visual Studio 不断创建已定义的事件处理程序

在将

大型(遗留(表单拆分为部分类并向其添加新部分后,我今天遇到了这个问题。尽管这是一个老问题,但我发布的是原始问题的许多答案都是不正确的。具体来说,他们中的许多人声称Visual Studio如果不双击就无法生成这些。我想确认VS在某些情况下确实会这样做。

在我的特定情况下,我向选项卡控件添加了一个新选项卡,并将新选项卡事件和代码放在新类中。 最初,我将此代码(包括事件(放在主窗体的末尾,这可能与该问题有关。移动代码后,如果我将控件添加到主窗体的任何部分(例如,单独的选项卡(,VS 会为我在新的分部类中的 3 个事件生成空事件处理程序,尽管此类中有完整的事件处理程序。

我现在唯一的解决方案是在出现问题时从主表单代码的底部删除空处理程序。溶液清洗不会删除这些空白处理程序。

由于所有 3 个每次

都一致地生成,并且由于它们位于我添加测试控件时隐藏在主窗体后面的选项卡上(因此没有双击的机会,当然没有机会双击所有 3 个(,我可以自信地说这是一个 Visual Studio 错误(在我的情况下是 VS 2013(。

更新 24/9/15:厌倦了删除处理程序后,我进行了更多搜索,并找到了解决问题的方法。这是来自MS论坛(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(的霉菌解决方案的文本。此修复程序对我有用,尽管单击创建事件仍然在错误的类中创建它们,但是在移动它们后,它们可以正常工作。它还可以防止创建无效的事件处理程序。

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.

在评论中,您声称:"如果我双击已经定义了事件处理程序的控件,它将带我到该事件处理程序,而不是创建一个新的事件处理程序。

如果手动挂钩事件,则不正确。

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

如果转到设计器并双击控件或在属性网格中分配事件(并且您会注意到它未在属性网格中标识(,Visual Studio 将在 InitializeComponent(( 和主窗体代码文件中创建一个完全不同的条目:

void myButton_Click1(...) { }

Visual Studio 永远不会自动创建控件事件处理程序,而无需您以某种方式参与设计器。 不幸的是,很容易导致VS Designer认为您双击了 - 在首次加载表单时经常长时间的暂停期间,最容易

显然,需要做的是清洁解决方案。显然,这会删除在幕后完成的所有序列化。(我不是Visual Studio如何进行序列化的专家,所以我可能会弄错术语。

Visual Studio (2008( "Clean Solution" 选项

你看,我妈不养傻瓜。