1

VB.NET フレームワーク 2.0 で記述された Windows フォーム アプリケーションがあります。

次の構造のコンテキスト メニューが関連付けられたグリッドがあります。

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

グリッド内で特定のキーが押されたときにコンテキスト メニューを表示し、'SubMenuItem1'プログラムで選択したいと考えています。

KeyUp次の方法で、グリッドのイベントからコンテキスト メニュー項目の Show() メソッドを呼び出して、コンテキスト メニューを表示できます。

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

ただし、サブメニューまたはサブサブメニューの項目をプログラムで選択する方法がわかりません。

誰でも助けることができますか?

4

1 に答える 1

1

誰かが 5 分以内に 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 に貼り付けるだけで、問題なく表示されるはずです。

于 2008-12-19T09:24:02.573 に答える