2016-07-16 67 views
0

我有一个菜单项包含5个子项,每个包含4个子子项,每个子项都会带一个新的形式,如果我想用新的形式编码需要我的物品4 * 5 = 20表格!!!!菜单项代码优化

是否有任何可能的方式,我可以知道所选的子子项的位置?然后我只能让一个形式,使得直到你拥有了一切

enter image description here

+0

是的。将所有项目放在窗体上,然后对控件使用Visible = false(或true)属性。 – jdweng

+0

这将只是隐藏项目,我想获得选定项目的索引或类似的东西,所以我可以根据选定的项目索引在一种形式对待所有的选择 –

回答

0

如果你想获得一个菜单项,从处理程序的位置,你可以继续工作了每一位业主的项目,并找到它的位置完整的职位列表:

// Get a reference to the current item as a tool strip menu item 
ToolStripMenuItem self = (ToolStripMenuItem)sender; 

// Build a list of positions 
List<int> position = new List<int>(); 
ToolStripMenuItem cur = self; 
// Keep looping until we don't find a parent 
while (cur != null) 
{ 
    if (cur.OwnerItem is ToolStripMenuItem) 
    { 
     // The owner is a menu item, add it's position to our list 
     ToolStripMenuItem parent = ((ToolStripMenuItem)cur.OwnerItem); 
     position.Insert(0, parent.DropDownItems.IndexOf(cur)); 
     // And now work on the owner 
     cur = parent; 
    } 
    else 
    { 
     // The owner isn't a menu item, so break out of our loop 
     cur = null; 
    } 
} 

// And as a demo, just show the positions: 
MessageBox.Show("You clicked on item at " + 
    string.Join(",", position.Select(x => x.ToString()).ToArray()));