且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# WinForm 多个单击事件处理程序用于类似功能

更新时间:2023-12-06 12:33:10

要做的第一件事是对每个处理程序使用相同的处理程序:

The first thing to do is use the same handler for each one:

toolStripMenu1.Click += toolStripItemClick;
toolStripMenu2.Click += toolStripItemClick;
// etc

我会为此使用 Tag 属性,在构建 toolStripItems 时设置它:

I would use the Tag property for this, set it when you're constructing the toolStripItems:

toolStripMenu1.Tag = "http://www.google.com";

然后定义您的处理程序:

And then define your handler:

private void toolStripItemClick(object sender, EventArgs e)
{
    var c = (ToolStripMenuItem)sender;
    Process.Start(c.Tag.ToString());
}