我正在尝试使用以下代码动态地将项添加到工具条中:
contextMenuStrip.Items.Add(string.Format("{0} kB/s", currSpeed), null, new EventHandler(Connection.SetSpeed));问题是我需要传递一个参数给Connection.SetSpeed: currSpeed (int)。我该怎么做呢?
耽误您时间,实在对不起。诚挚的问候。
发布于 2009-03-05 12:31:23
调用add会返回一个ToolStripItem,如果您将它的Tag属性设置为currSpeed变量,那么当项目被单击时,您应该能够通过Connection.SetSpeed方法中的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...
}https://stackoverflow.com/questions/614482
复制相似问题