开发者

How to use ToolStripMenuItems without mouse events ( click or double Click event )?

开发者 https://www.devze.com 2022-12-25 08:19 出处:网络
I have a solution in Vb.NET 2008 windows Form with ToolStripMenuItems, but I need to implement menu navigation without any mouse intervention.

I have a solution in Vb.NET 2008 windows Form with ToolStripMenuItems,

but I need to implement menu navigation without any mouse intervention.

I only found mouse events In ToolStripMenuItems, MenuStrip, etc.

All MenuStrip and ToolStripMenuItems are created programatically, reading user permissions from Database, then add a handler to manage the events, but the client don't want mouse interaction, he wants only keyboard inputs only !.

Add menu routine:

    Private Sub AddModulo(ByVal strNAME As String, ByVal strTITULO As String)

    Dim oToolStripMenuItem As New ToolStripMenuItem(strTITULO)

    oToolStripMenuItem.Name = strNAME
    oToolStripMenuItem.Text = "&" & strTITULO


    mnuPrincipal.Items.Add(oToolStripMenuItem)
    AddHandler oToolStripMenuItem.MouseMove, AddressOf LoadMenu


End Sub

Add item routine:

    Private Sub AddItems(ByVal StrModulo As String, ByVal strItem As String)

    Dim mnuItem As New ToolStripMenuItem开发者_如何学运维

    mnuItem.Name = StrModulo.ToUpper
    mnuItem.Size = New System.Drawing.Size(145, 22)

    If Microsoft.VisualBasic.Left(strItem, 1) = "-" Then

        mnuItem.Text = "                " & Microsoft.VisualBasic.Mid(strItem.ToUpper, 2)
        mnuItem.BackColor = Color.Blue
        mnuItem.ForeColor = Color.White

    Else
        mnuItem.Text = strItem
    End If

    mnuItem.Tag = UCase(Mid(StrModulo, 5, 2))

    AddHandler mnuItem.Click, AddressOf LoadMenuItem

    Dim newOption = mnuPrincipal.Items(mnuPrincipal.Items.Count - 1)
    newOption.DropDownItems.Add(mnuItem)

End Sub

Thx.


This is very basic stuff so if this is not what you need just disregard this answer but it appears to me that this is what you are asking about.

Anyway, ToolStripMenuItems are easily accessible using the keyboard...

If you insert a MenuStrip into your form from the toolbox and than use the 'Insert Standard Items' option from MenuStrip Tasks to insert standard Menu items, when you launch your winforms application, if you press the Alt key you'll notice that a letter from each top level ToolStripMenuItem is underlined. This alerts you that sub items of that ToolStripMenuItem are available if you press that key combined with the Alt key. For example, if you press Alt+F, you'll drop the File menu.

After the top level menu is dropped this way, you can press one more keyboard key to access the sub items. This is accomplished by adding a & char in front of any letter in a ToolStripMenuItem's 'Text' property in the designer. This allows you to access a sub item after using the Alt+Key combination to drop the top level menu. For example, by default OpenToolStripMenuItem which is a sub item of the File Menu has & char in front of the letter O (&Open) so your users can easily access this event by pressing Alt+F+O instead of using the mouse.

In addition to that, you can easily assign a shortcut key to each ToolStripMenuItem by using the 'ShortcutKeys' property of the ToolStripMenuItem. If you drop down the selected ToolStripMenuItem's 'ShortcutKeys' property in the designer you'll be able to choose the modifier (Ctrl, Shift, Alt) and the keyboard key you'd like to use in combination with that modifier (usually Ctrl+O for File>Open). Using that same approach you can assign a keyboard shortcut key to any ToolStripMenuItem that you add to the menu strip yourself...

Hope this helps...


Use the shortcuts as suggested, but also set TabStop to True on your base menu container. It will allow the keyboard to access the menu via tab and arrow keys.

You should be able to use a combo of the mousedown and the click event to test for the "Enter key". Add a boolean that you can set to true in the mousedown event (fires before click) that determines if the mouse was used to click the button. Then in the Click event check for the boolean and exit sub after clearing the boolean. If the mousedown boolean isn't set you pressed the enter key to trigger the event.

0

精彩评论

暂无评论...
验证码 换一张
取 消