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
        fileItem.DropDownItems.Add(filesubitem1)
        menustrip.Items.Add(fileItem)
        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
    fileMenu.DropDownItems.Add(fileQuit)

    mainMenu.Items.Add(fileMenu)

    AddHandler fileQuit.Click, AddressOf QuitToolStripMenuItem_Click
    Me.Controls.Add(mainMenu)

End Sub

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

End Class

Hi

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)

HTH

commented: Quite right. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.