I would like to assign a CTRL Q shortcut to the Quit submenuitem (filesubitem1) but can't find
the right syntax. Any assistance will be greatly appreciated. Thank you.

    If user_wants_file_menu = True Then
        Dim fileItem As ToolStripMenuItem = New ToolStripMenuItem("File")
        Dim filesubitem1 As ToolStripMenuItem = New ToolStripMenuItem("Quit")
        filesubitem1.ShowShortcutKeys = True
        filesubitem1.ShortcutKeys = Shortcut.CtrlQ  'this errors at compile time
        AddHandler filesubitem1.Click, AddressOf QuitToolStripMenuItem_Click_1
    End If

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Dim mainMenu As New MenuStrip
    Dim fileMenu As New ToolStripMenuItem("File")
    Dim fileQuit As New ToolStripMenuItem("Quit")

    fileQuit.ShowShortcutKeys = True
    fileQuit.ShortcutKeys = Shortcut.CtrlQ


    AddHandler fileQuit.Click, AddressOf QuitToolStripMenuItem_Click

End Sub

Private Sub QuitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs)
End Sub

End Class

Edited 1 Year Ago by Reverend Jim


I'm guessing you have Option Strict On (which is great) as if it was Off then both your code and that posted by Reverend Jim would work fine. But with Option Strict On you get a conversion error from System.Windows.Forms.Shortcut to System.Windows.Forms.Keys.

To fix, you need to cast the shortcut key to System.Windows.Forms.Keys:

filesubitem1.ShortcutKeys = DirectCast(Shortcut.CtrlQ, Keys)


Quite right.
This question has already been answered. Start a new discussion instead.