2008-12-19 17 views
1

我有一個用VB.NET框架2.0編寫的Windows窗體應用程序。在.NET中通過上下文菜單進行編程導航WinForms Framework 2.0

我有具有以下結構的相關聯的上下文菜單中選擇一個網格:

MenuItem1 
MenuItem2 --> 
      SubMenuItem1 
      SubMenuItem2 --> 
          SubSubMenuItem1 
MenuItem3 
... 

我希望當特定鍵被網格內壓以顯示上下文菜單,並具有'SubMenuItem1'選定編程。

我可以通過以下方式從電網的KeyUp事件調用上下文菜單項Show()方法顯示上下文菜單:

contextMenu.Show(MainForm.GetSingleton(), Cursor.Position) 

我卻無法弄清楚如何以編程方式選擇子菜單或子子菜單中的項目。

任何人都可以幫忙嗎?

回答

1

如果某人在五分鐘內出現類似如下內容的東西,這可能是最大的最醜陋的代碼:ToolStripMenuItem8.selectAllParents()。但據我所知,沒有這樣的功能。

因此,這裏是我能想出:

Private Sub openTSMitem(ByVal menu As ContextMenuStrip, ByVal selectitem As ToolStripMenuItem) 

    'The menu needs to be open befor we call ShowDropDown 
    menu.Show() 

    'The list will first contain the parents in the order of bottom to top 
    'then we will reverse it so we can open the submenus from top to bottom 
    'otherwise it will not open them all 
    Dim parentsRevOrder As ArrayList = New ArrayList() 

    'Add the parents to the list 
    Dim parentItem As ToolStripMenuItem = selectitem.OwnerItem 
    While Not parentItem Is Nothing 
     parentsRevOrder.Add(parentItem) 
     parentItem = parentItem.OwnerItem 
    End While 
    'reverse the list. now its in the order top to bottom 
    'and the submenus will open correctly 
    parentsRevOrder.Reverse() 

    'now loop through and open the submenus 
    For Each tsiParent As ToolStripMenuItem In parentsRevOrder 
     tsiParent.ShowDropDown() 
    Next 

    'and finally select the menuItem we want 
    selectitem.Select() 

End Sub 

,然後調用子:

openTSMitem(ContextMenuStrip1, ToolStripMenuItem8) 

希望它能幫助。

編輯:我剛纔看到的評論和代碼顯示了位在回答混合,只需將其粘貼到Visual Studio雖然,這應該蠻好看的

+0

感謝。我會看看這個並回復你,但是可能需要一兩天才能完成。 – Jayden 2008-12-19 21:59:21