我有两个类,每个类都有自己的ContextMenuStrip
。
由于每个Obejct都可以从另一个访问,我想嵌入一个对象的每个ToolStripMenuItem
,可以在其他DropDown
的{{1}}中使用。
对象属于ContextMenuStrip
和MAC
类型
这是MAC TCPIP
:
ContextMenuStrip
我的问题是,如果我正在使用TCPIP onbject并且正在访问ContextMenuStrip,那么它会立即打开。 (按预期)
当我调用MAC ContextMenuStrip cms = new ContextMenuStrip();
foreach (TCPIP ip in AssignedTCPIPentries) {
mi = new ToolStripMenuItem(ip.IP + " - " + ip.Hostname);
for (int t = 0; t < ip.SingleItemContextMenue.Items.Count; t++) {
mi.DropDown.Items.Add(ip.SingleItemContextMenue.Items[t]);
}
cms.Items.Add(mi);
}
并希望将每个分配的TCPIP对象的每个ContextMenuStrip
嵌入到子菜单中时(如上面的代码所示)它需要Ages(每个TCPIP对象大约10秒)完成(每个TCPIP obejct有21个ToolStripMenuItems)。但为什么表现如此糟糕,我该如何避免呢?
TCPIP.SingleItemContextMenue部分的片段:
ContextMenuStrip.Item
PS:public ContextMenuStrip SingleItemContextMenue {
get {
ContextMenuStrip cms = new ContextMenuStrip();
ToolStripMenuItem mi = new ToolStripMenuItem();
mi.Text = "IP Infos [" + IP + "]";
mi.Click += new EventHandler(showDetailedInformation);
mi.Image = Toolbox.Properties.Resources.Info_64;
cms.Items.Add(mi);
cms.Items.Add(new ToolStripSeparator()); // Horizontal line
mi = new ToolStripMenuItem("Copy IP [" + IP + "]");
mi.Image = Toolbox.Properties.Resources.Copy_64;
mi.Click += new EventHandler((ss, e) => general.copyStringIntoClipboard(ss, e, IP));
cms.Items.Add(mi);
//And some more...
return cms;
}
}
由于枚举问题而无法通过foreach
进行迭代。但我也没有理由这样做。但也许它与我的问题有关系吗?
答案 0 :(得分:1)
每次使用SingleItemContextMenue
ip.SingleItemContextMenue
getter
ContextMenuStrip cms = new ContextMenuStrip();
foreach (TCPIP ip in AssignedTCPIPentries) {
mi = new ToolStripMenuItem(ip.IP + " - " + ip.Hostname);
var ipSingleItemContextMenueItems = ip.SingleItemContextMenue.Items; // get items
for (int t = 0; t < ipSingleItemContextMenueItems.Count; t++) {
mi.DropDown.Items.Add(ipSingleItemContextMenueItems[t]);
}
cms.Items.Add(mi);
}