2012-05-04 56 views
1

我有一個Windows窗體已在多個部分類文件中定義。在其中一個文件(xxx.Query.cs)中,我有爲DataGridView定義的所有事件處理程序。但是,有時Visual Studio會決定爲表單(xxx.cs)的主文件中的四個事件處理程序創建存根。我最終得到了四個錯誤,其中說「類型'xxx'的行已經定義了一個名爲'dataGridView1_ColumnHeaderMouseClick'的成員具有相同的參數類型」。Visual Studio不斷創建已定義的事件處理程序

我該如何讓Visual Studio退出嘗試爲這些創建存根?這是最令人頭疼的。

+4

VS不會任意創建存根。在這種情況發生之前你是否雙擊控件? –

+1

如果您使用表單設計器,如果您(雙擊)單擊設計器中的按鈕,它將自動創建這些表單。 – yoozer8

+1

首先,如果我雙擊已經定義了事件處理程序的控件,它將帶我到那個事件處理程序,而不是創建一個新的。無論如何,這並不重要,因爲我沒有點擊控件。 –

回答

1

顯然,需要做的是清理解決方案。這顯然會消除在幕後完成的所有序列化。 (我在Studio如何不可視其系列化的專家,所以我可以有術語錯誤。)

Visual Studio (2008) 'Clean Solution' Option

看,我的媽媽不提高沒有傻瓜。

+0

這很不幸沒有爲我解決任何問題,新的事件仍然在錯誤的類中創建,並且無效的處理程序一直在創建。我上面發佈了一個工作解決方案。 – WiredEarp

+0

我不這麼認爲......「Clean」命令會刪除「bin」目錄中的對象,調試信息和二進制文件。它不應該觸摸源代碼。 – tedebus

2

在評論中,您聲稱:「如果我雙擊已經定義了事件處理程序的控件,它將帶我到該事件處理程序,而不是創建一個新的。

如果您手動掛鉤事件,則不是這樣。

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

void myButton_Click(...) { } 

如果你去的設計師,要麼雙擊控制或分配屬性網格中的事件(你會注意到它不是在屬性網格標識),Visual Studio將創建一個完全的附加條目的InitializeComponent()和主窗體代碼文件:

void myButton_Click1(...) { } 

Visual Studio將不會自動在沒有設計師某種程度上你的參與創建控件事件處理。不幸的是,VS Designer很容易讓你認爲你是雙擊的 - 在第一次加載表單時,最容易出現這種長時間的停頓。

+0

我喜歡答案,但我沒有手動定義這些事件處理程序;事件處理程序在xxx.Designer.cs中設置。實際上,當我雙擊控件時,它會將我帶到我已經定義的方法:dataGridView1_CellContentClick。 –

2

我在將大型(遺留)表單拆分爲部分類之後,今天遇到了此問題,同時向其中添加了新的部分。 儘管這是一個古老的問題,但我發帖的原因很多,答案都不正確。具體而言,他們中的許多人聲稱,如果沒有雙擊,Visual Studio無法生成這些內容。我想確認在某些情況下,VS 確實是

在我的特殊情況下,我向選項卡控件添加了一個新選項卡,並將新選項卡事件和代碼放入新類中。最初,我已將此代碼(包括事件)置於主窗體的末尾,其中可能與有關。在移動代碼之後,如果我將控件添加到主窗體的任何部分(例如單獨的選項卡),VS會爲我的新分部類中的3個事件生成空事件處理程序,儘管此時具有完整的事件處理程序類。

我現在唯一的解決方案是在發生問題時從主窗體代碼的底部刪除空處理程序。解決方案清理不會刪除這些空白處理程序。

因爲所有3個每次都一致地生成,並且由於它們在添加測試控件時隱藏在主窗體後面的選項卡上(所以沒有雙擊的機會,當然沒有雙擊所有3的機會),我可以放心地說,這是一個Visual Studio錯誤(VS 2013在我的情況下)。

UPDATE 24/9/15:在厭惡刪除處理程序後,我做了一些更多的搜索,並找到了解決方案。下面是來自MS論壇的mustiy解決方案的文本(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. 
相關問題