2016-07-16 21 views
0

私は5つのサブアイテムを含むメニューアイテムを持っています。それぞれ1つの新しいサブフォームがあります。私は4 * 5 = 20のフォームを取るアイテム!!!!メニューアイテムコードの最適化

選択されたサブサブアイテムの位置を知る方法はありますか?その後、私はあなたが持っているまで、すべてenter image description here

+0

はい。フォーム上にすべてのアイテムを配置し、コントロールのVisible = false(またはtrue)プロパティを使用します。 – jdweng

+0

は、アイテムを隠すだけで、選択したアイテムのインデックスなどを取得したいので、選択したアイテムインデックスに基づいてすべての選択肢を1つのフォームで扱うことができます –

答えて

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()));