Dynamically building shortcuts on dynamic menus

jack-gray

Member
Joined
Sep 12, 2006
Messages
6
Location
Minneapolis, MN
Programming Experience
Beginner
Hi All,
I have a form that has a Main Menu control and I am adding items to the Menu based on user information stored in a text file. I allow the user to select the shortcut key they would like to use from a combobox and then select the application they would like to start from a browse button. The text file stores the string value of the shortcut key (ex. "F3") as well as the .exe file and the path that the file is stored in.
The code for the dynamic menu is...

'add dynamic menus
Dim intcounter As Int32
Call ReadMenuItemsTextFile()
If m_colDynamicApp.Count > 0 Then
'add the Main Menu item below
Me.m_mnuApplications.Visible = True
Me.m_mnuApplications.Index = 2
For intcounter = 1 To m_colDynamicApp.Count
'add the additional items below the Main Menu item
Me.m_mnuApplications.MenuItems.Add _
(m_colDynamicApp.Item(intcounter), _
New EventHandler(AddressOf MenuItem_OpenTool))
'add shortcut information

Next
End If

What I would like to do is use the stored string from the combobox to dynamically add the users choice for the shortcut key. Is there any way to do this in vb.net

Thanks,
 
Are you using VB 2005? If so then use a MenuStrip rather than a MainMenu. If not then please update your profile accordingly to avoid confusion.

The Shortcut property of the menuItem class is a value from the Shortcut enumeration. Instead of storing a string like "F3" you should store the numerical value of the Shortcut member selected. When you read the value from the text file as a string you can then convert it to an Integer and cast that directly as a Shortcut value.

As an example, the value Shortcut.F3, which represents the F3 key, has a numerical value of 114. You store 114 as a string in your text file and then convert it to a Shortcut value like this:
VB.NET:
Dim shortcutString As String = "114" 'Read from file
Dim shortcutNumber As Integer = Integer.Parse(shortcutString) 'Now contains the number 114
Dim shortcutValue As Shortcut = DirectCast(shortcutNumber, Shortcut) 'Now contains the value Shortcut.F3

myMenuItem.Shortcut = shortcutValue
You should read about the MenuItem.Shortcut property and the Shortcut enumeration for more information.
 
Thanks jmcilhinney,
I have gotten this piece written and yes I am storing the integer like you stated. I then use a rather large select statement to determine the shortcut that the user has chosen.
Thanks so much for your help.
jack-gray
 
You shouldn't need to use a Select Case statement. You can fill a ComboBox with actual Shortcut values and when the suer makes a selection the SelectedItem of the ComboBox IS a Shortcut value:
VB.NET:
Me.ComboBox1.DataSource = [Enum].GetValues(GetType(Shortcut))
If you want to make what's displayed slightly more user-friendly then you could do this:
VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim shortcuts As Shortcut() = DirectCast([Enum].GetValues(GetType(Shortcut)), Shortcut())
        Dim upperBound As Integer = shortcuts.GetUpperBound(0)
        Dim friendlyShortcuts(upperBound) As UserFriendlyShortcut

        For i As Integer = 0 To upperBound Step 1
            friendlyShortcuts(i) = New UserFriendlyShortcut(shortcuts(i))
        Next i

        Me.ComboBox1.DataSource = friendlyShortcuts
    End Sub
End Class

Public Structure UserFriendlyShortcut

    Private _shortcut As Shortcut

    Public Property Shortcut() As Shortcut
        Get
            Return Me._shortcut
        End Get
        Set(ByVal value As Shortcut)
            Me._shortcut = value
        End Set
    End Property

    Public Sub New(ByVal shortcut As Shortcut)
        Me._shortcut = shortcut
    End Sub

    Public Overrides Function ToString() As String
        Return Me._shortcut.ToString().Replace("Ctrl", "Ctrl+").Replace("Shift", "Shift+").Replace("Alt", "Alt+")
    End Function

End Structure
Try them both and see the results. In the first case you would assign the chosen shortcut to a menu item like this:
VB.NET:
myMenuItem.Shortcut = DirectCast(myComboBox.SelectedItem, Shortcut)
In the second case you'd do it like this:
VB.NET:
myMenuItem.Shortcut = DirectCast(myComboBox.SelectedItem, UserFriendlyShortcut).Shortcut
 
But what if...

Wow that's awesome :p I am wondering though if I wanted to continually update the list based on a users choice. For example, a user selects F5 as their shortcut key I would like to store that information in my text file so that the next time the form loads they are not given the choice of using F5 as a shortcut because it is already used and also to dynamically update the list so they are not even given that choice when they look in the list for the next selection. I know that once I have the values stored properly that I only have to make a call to the sub Private Sub LoadComboShortcuts() again. This way I might even be able to limit their choices (I don't know why) of the shortcuts they can choose from at the start.
Thanks so much.
 
Okay, I have answered my own question. This ends up getting stored as an integer and when I return that integer I CType it back into a shortcut
MyShortcut = CType(MyInteger, shortcut).
That's it.
Thanks all,
Jack
 
Back
Top