VB.NET - Menu Shortcuts

  • Thread starter Thread starter Brano
  • Start date Start date
B

Brano

Hi all,

I have this problem I have a menu structure in my VB.NET application
that is about 4 levels deep. I need an CTRL+key access to the 3rd
level so that if user press CTRL+key the 3rd menu rolls out

The standart code :

myMenuItem.Shortcut = Shortcut.CtrlA

does not work it only works for the final item in the menu that has an
event handler but this is what I need to do...

Anybody any suggestions?
 
Hi all,

I have this problem I have a menu structure in my VB.NET application
that is about 4 levels deep. I need an CTRL+key access to the 3rd
level so that if user press CTRL+key the 3rd menu rolls out

The standart code :

myMenuItem.Shortcut = Shortcut.CtrlA

does not work it only works for the final item in the menu that has an
event handler but this is what I need to do...

Anybody any suggestions?

AFAIK a main menu cannot be opened directly from code (though SendKeys
might help, or you could send windows messages simuating mouse clicks), but
you can clone the submenu you want to open into a context menu.

It's not as pretty a solution as opening the real thing, as the
higher-level menus aren't shown.

Suppose the submenu you want to open is called mnuX, and you want to assign
key F8 to it. Instead of setting F8 as shortcut key, set the form's
KeyPreview property to True, and code it like this:

Private Sub Form1_KeyDown(ByVal sender As Object, ....
If e.KeyCode = Keys.F8 Then
Dim cx As New ContextMenu
For Each mi As MenuItem In mnuX.MenuItems
cx.MenuItems.Add(mi.CloneMenu)
Next
cx.Show(Me, New Point(0, 0))
e.Handled = True
End If
End Sub

You don't need to call AddHandler for the cloned menu items, they inherit
the handlers of the originals.
 
Hi I have tried your code but It does not seems to even pickup pressing
of F8 key as an event... I mean if I put the code under Form_KeyDown
Replace what is after Then statement with msgbox("F8") run it and then
press F8 nothing happens...

and the next thing my menus are build from the code like this :

'# creating variables to hold default menu items
Dim TestsItem As New MenuItem
Dim AddTestItem As New MenuItem
Dim DeleteTestItem As New MenuItem
Dim MicrobiologyItem As New MenuItem
Dim ChemistryItem As New MenuItem
Dim GroupTestsItem As New MenuItem

'# assigning them text
TestsItem.Text = "&Tests"
AddTestItem.Text = "&Add Test"
DeleteTestItem.Text = "&Delete Test"
MicrobiologyItem.Text = "&Microbiology"
MicrobiologyItem.Shortcut = Shortcut.CtrlA
MicrobiologyItem.ShowShortcut = True
ChemistryItem.Text = "&Chemistry"
ChemistryItem.Shortcut = Shortcut.F9
ChemistryItem.ShowShortcut = True
GroupTestsItem.Text = "&Group Tests"
GroupTestsItem.Shortcut = Shortcut.F11
GroupTestsItem.ShowShortcut = True

'# adding them to the menu
thisMenu.MenuItems.Add(TestsItem)
TestsItem.MenuItems.Add(AddTestItem)
TestsItem.MenuItems.Add(DeleteTestItem)
AddTestItem.MenuItems.Add(MicrobiologyItem)
AddTestItem.MenuItems.Add(ChemistryItem)
AddTestItem.MenuItems.Add(GroupTestsItem)

there is more code that populates

MicrobiologyItem
ChemistryItem
GroupTestsItem

with loads of another stuff from Database (about another 50
items) none of them has any more subitems


What will the code look like to clone every item from MicrobiologyItem
??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top