Winforms 使用DynamicToolStripItem进行事件处理

Winforms 使用DynamicToolStripItem进行事件处理,winforms,event-handling,Winforms,Event Handling,我正尝试使用以下代码将项目动态添加到toolstrip: contextMenuStrip.Items.Add(string.Format("{0} kB/s", currSpeed), null, new EventHandler(Connection.SetSpeed)); 问题是我需要向Connection.SetSpeed:currSpeed(int)传递一个参数。 我该怎么做 谢谢你抽出时间。 致以最诚挚的问候。调用add将返回ToolStripItem,如果将其Tag属性设置为cu

我正尝试使用以下代码将项目动态添加到toolstrip:

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...
}

调用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...
}