我正在尝试使用以下代码动态添加项目到工具条:

contextMenuStrip.Items.Add(string.Format("{0} kB/s", currSpeed), null, new EventHandler(Connection.SetSpeed));

问题是我需要将参数传递给Connection.SetSpeed:currSpeed(int)。 我怎么能这样做?

感谢您的时间。 最好的问候。

有帮助吗?

解决方案

调用add会返回一个ToolStripItem,如果你将它的Tag属性设置为currSpeed变量,你应该能够在单击该项时通过Connection.SetSpeed方法中的sender参数拉出该ToolStripItem ...

ToolStripItem item = contextMenuStrip.Items.Add(string.Format("{0} kB/s", currSpeed), null, new EventHandler(Connection.SetSpeed));
item.Tag = currSpeed;

void Connection.SetSpeed (object sender, EventArgs e)
{
    ToolStripItem item = (ToolStripItem)sender;
    int currSpeed = (int)item.Tag;

    // Do stuff...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top