2016-07-16 76 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()));