Results 1 to 2 of 2

Thread: [2003] Strings and Keys

  1. #1

    Thread Starter
    Lively Member therat324's Avatar
    Join Date
    Oct 2008
    Location
    Bethany, Oklahoma
    Posts
    94

    [2003] Strings and Keys

    In the program I have the user can select a hotkey from a combo box (Shift,ctrl,alt...ect) and when they save their settings I save the variable as a string

    Code:
    _Hotkey = HotkeyCOMBO.selectedItem 'Saves users choice to string variable
    How do I get the string(_Hotkey) into a format that this can read

    Code:
    If e.KeyData = _hotkey Then
    I searched the forums and one person was using a DataSource. Is that the only way to do it or can you convert it just by having the string of the key you need to use?
    My car if Chevy was VB.NET

    Code:
    Public Class MyCar
        Public Sub Camaro()
            Dim Camaro As Car
            Camaro = SS.Fast("Sexy")
        End Sub
    
        Public Function Fast(ByVal sexy As String ) As String
            Return "SUPER" & sexy
        End Function
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2003] Strings and Keys

    I would suggest that you do this the way VS does. In your UI, give the user a list of possible keys to choose from and then use CheckBoxes to select Ctrl, Shift and Alt. You can then store four values: Booleans for Ctrl, Shift and Alt and a String for the key. Your test will then look like this:
    vb.net Code:
    1. If e.KeyCode = DirectCast([Enum].Parse(GetType(Keys), key), Keys) AndAlso _
    2.    e.Control = ctrl AndAlso _
    3.    e.Shift = shift AndAlso _
    4.    e.Alt = alt Then
    5.     'The hotkey was detected.
    6. End If
    An even better idea would be to create a composite Keys value when the user makes there selection and store that:
    vb.net Code:
    1. Dim hotkey As Keys = DirectCast([Enum].Parse(GetType(Keys), Me.keyComboBox.Text), Keys)
    2.  
    3. If Me.ctrlCheckBox.Checked Then
    4.     hotkey = hotkey Or Keys.Control
    5. End If
    6.  
    7. If Me.shiftCheckBox.Checked Then
    8.     hotkey = hotkey Or Keys.Shift
    9. End If
    10.  
    11. If Me.altCheckBox.Checked Then
    12.     hotkey = hotkey Or Keys.Alt
    13. End If
    You can then compare that value to e.KeyData in your KeyDown event handler.

    A better idea still would be to populate your ComboBox with Keys values in the first place. If you put enumerated constants in a ComboBox then it will display their text labels, but the SelectedItem will still be a Keys value, so there's no need to parse a string:
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width