Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在VisualStudio中删除未使用的C代码_C#_Winforms_Visual Studio_Mouseclick Event - Fatal编程技术网

C# 在VisualStudio中删除未使用的C代码

C# 在VisualStudio中删除未使用的C代码,c#,winforms,visual-studio,mouseclick-event,C#,Winforms,Visual Studio,Mouseclick Event,在处理windows窗体时,我意外地单击了按钮,现在我有了与此单击事件相关的部分代码。我不需要它们,我想从代码中删除这些部分,但如果我这样做了,VisualStudio会在编译时抱怨,因为它会搜索丢失的代码。如何消除代码中未使用的单击事件 双击控件时,会连接默认事件,并为您创建一个存根处理程序 您看到并删除的存根处理程序 private void button1_Click(object sender, EventArgs e) { } 另一部分是事件实际连接的地方。这就是编译错误的来源。您已

在处理windows窗体时,我意外地单击了按钮,现在我有了与此单击事件相关的部分代码。我不需要它们,我想从代码中删除这些部分,但如果我这样做了,VisualStudio会在编译时抱怨,因为它会搜索丢失的代码。如何消除代码中未使用的单击事件

双击控件时,会连接默认事件,并为您创建一个存根处理程序

您看到并删除的存根处理程序

private void button1_Click(object sender, EventArgs e)
{
}
另一部分是事件实际连接的地方。这就是编译错误的来源。您已删除事件处理程序,但未删除事件订阅

这可以在特定表单所附的Designer.cs文件中找到

private void InitializeComponent()
{
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Name = "button1";

    //This is the line that has the compile error.
    this.button1.Click += new System.EventHandler(this.button1_Click);
}

如注释中所述,您可以转到该控件的事件属性并重置事件,但也可以转到设计器并删除无效行。使用Reset命令将删除存根和事件订阅。

以下是另一种解决方案:

转到属性按F4,然后转到事件。 选择不需要的事件,然后右键单击事件图标。 单击“重置”将其自动从designer.cs中删除。 在.cs文件中手动删除它。
谢谢。

当Visual Studio给出此错误时,它是否引用了设计器文件中可能存在的特定代码行?转到该行代码并删除对已删除事件处理程序的引用。如果您不想删除该语句,请使用简单的方法,然后返回属性窗口,单击闪电图标,右键单击事件并选择重置。谢谢,正如建议的那样,我只需删除对已删除事件处理程序的引用。现在看来效果不错。谢谢大家。仅供参考-您可以通过在属性窗口中单击事件列并按Delete键来删除它。仅当其iirc为空时,此选项才起作用。可能的