c# - 在 Visual Studio 中删除未使用的 C# 代码

标签 c# winforms visual-studio mouseclick-event

在 Windows 窗体上工作时,我不小心单击了按钮,现在我有了与此单击事件相关的部分代码。我不需要它们,我想从代码中删除这些部分,但如果我这样做,Visual Studio 会在编译时提示,因为它会搜索丢失的代码。我怎样才能摆脱我的代码中未使用的点击事件?

最佳答案

当您双击一个控件时,系统会连接默认事件并为您创建一个已删除的处理程序。

您所知道的 stub 处理程序,如您所见并已删除。

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 命令将删除 stub 和事件订阅。

关于c# - 在 Visual Studio 中删除未使用的 C# 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26187703/

相关文章:

c# - 更新数据库中的第一行 C# mysql

c# - 在 WinForms 应用程序中记录所有按钮点击

c# - OData v4 路由前缀?

c# - 闪烁的 ToolStripButton

c# - 按钮列的 DataGridView 图像

Visual Studio 2015 中的 Git (SSH)

android - Xamarin RecyclerView Click 事件获取多个上下文

c++ - 如何使用 <array> header 声明二维数组?

C# SwaggerGen + AutoRest - 如何发送流主体?

c# - WPF 使用 .Net 3.1 - Process.Start Excel 文件错误 : "The specified executable is not a valid application for this OS platform."