Results 1 to 8 of 8

Thread: SOLVED : CTRL+S on combobox

  1. #1
    Stonkie is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Sep 2007
    Posts
    279
    Reputation
    133

    SOLVED : CTRL+S on combobox

    I want to handle the CTRL+S key press on my form to save the data on it like most applications support. I set the KeyPreview property of my form to true and I handle the keydown event like this :

    Code:
    If e.Control AndAlso e.KeyCode = Keys.S Then
        piclistCurrentPlant.Focus()
    
        this.SaveCurrentPlant()
        e.Handled = True
    End If
    Things work fine, except when I do this when a combo box has the focus, it selects the first value beginning with the letter 's'. Is there some way to either disable this on the combo box or otherwise tell the form the event was handled?

    EDIT : Some experimentation showed that I could prevent this by handling the KeyPress event and setting the Handled property to true. I don't have a value telling me the Control button is pushed, but I should be able to figure that out. Is this the common method of doing this?

    By the way, this is not on my main form, it is the details form of the specific record selected by the user in the list on the main form. I'd like to avoid global key hooks and win32 api calls as much as possible...

    EDIT2 : I came up with this content for the KeyPress event handler :

    Code:
    If e.KeyChar = (Char)(Keys.S And Keys.Control) Then
        e.Handled = True
    End If
    I left the initial KeyDown event handler and things seem to be working as I expected. I also added something to mark the KeyUp event as handled if it's the Ctrl+S combination.

    I guess I solved my own problem! If anyone has a different, simpler way, feel free to show off! It's my first time looking for this behavior, but I'm sure to be adding similar keyboard shortcuts for various options now that the software is working/tested and we're in the adding stuff phase (the first application I managed which actually finished before the deadline! ).

    EDIT3: Translated the code from C# to VB.NET.
    Last edited by Stonkie; 11-13-2008 at 5:26 PM.
    The human mind's capability to comprehend abstract concepts is limited to the vocabulary it can use to describe it. The more precise the building blocks, the more complex the thoughts that can emerge... Sounds like the evolution of programming languages doesn't it?

  2. #2
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,362
    Reputation
    974
    I love the C# code and you do realize that this is a vb.net forum, right?

    Anywho languages aside, you could set boolean value in the keydown event for the control key then you could handle the KeyPress event setting e.Handled to false if the boolean is true and the keychar is the "s" key then put the code you have in your post in the keyup event instead of the keydown event, also in the keyup event be sure to set your control key boolean to false again
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  3. #3
    Stonkie is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Sep 2007
    Posts
    279
    Reputation
    133
    First, thanks for the reply!

    Sorry about the C#, I haven't done VB.NET in a while, but I kept the same resource forums because you helped me out a lot guys! I just didn't think to translate this before I posted... I'll do it right after this post.

    I thought about the boolean variable to keep track of the control key press, but I figured if the user holds ctrl outside the form and then clicks on the form and presses 'S', the keydown event won't occur... I could use an isKeyPressForSaving and add it to the same KeyDown event handler which causes the save so the control key would still reach the controls on the form when I am not saving though... Still, my current solution is cleaner without the need for a new member on my form.

    Also, you would put the call to the save method in the KeyUp instead of KeyDown? I tried this in MS Word and the save dialog seems to appear when you push the key, not when it is released...
    The human mind's capability to comprehend abstract concepts is limited to the vocabulary it can use to describe it. The more precise the building blocks, the more complex the thoughts that can emerge... Sounds like the evolution of programming languages doesn't it?

  4. #4
    Stonkie is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Sep 2007
    Posts
    279
    Reputation
    133
    My condition didn't actually work to tell the control key was pushed. Bit fields aren't added with "And", but with "Or". And even then, it doesn't work any better : it doesn't seem made for that. I thougth it worked for some strange reason

    The msdn article says I can tell the Control key was pressed with it, but no event is fired for the Control key event if the event argument supports it : KeyPressEventArgs.KeyChar Property (System.Windows.Forms)

    That's my understanding anyway... I had to rely on the isKeyPressForSaving boolean member and setting it to true or false depending on the keys from the KeyDown event.

    I wonder how such a common use case can not have a best practice solution...
    The human mind's capability to comprehend abstract concepts is limited to the vocabulary it can use to describe it. The more precise the building blocks, the more complex the thoughts that can emerge... Sounds like the evolution of programming languages doesn't it?

  5. #5
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,362
    Reputation
    974
    This is what I'm talking about:
    Code:
        Private m_ComboBoxCtrlKeyPressed As Boolean = False
    
        Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
            m_ComboBoxCtrlKeyPressed = e.Control
        End Sub
    
        Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
            e.Handled = m_ComboBoxCtrlKeyPressed = True AndAlso Char.ToLower(e.KeyChar) = "e"
        End Sub
    
        Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
            m_ComboBoxCtrlKeyPressed = False
            'Your C# code here as vb.net code
        End Sub
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  6. #6
    Stonkie is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Sep 2007
    Posts
    279
    Reputation
    133
    Alright, thanks!
    The human mind's capability to comprehend abstract concepts is limited to the vocabulary it can use to describe it. The more precise the building blocks, the more complex the thoughts that can emerge... Sounds like the evolution of programming languages doesn't it?

  7. #7
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,471
    Reputation
    2732
    Code:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.Control AndAlso e.KeyCode = Keys.S Then
            e.SuppressKeyPress = True
    
        End If
    End Sub

  8. #8
    Stonkie is offline VB.NET Forum Master
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Sep 2007
    Posts
    279
    Reputation
    133
    That was so obvious, I just saw the "Handled" property and never considered there would be one for this! Thanks a lot, that makes things quite simpler.
    The human mind's capability to comprehend abstract concepts is limited to the vocabulary it can use to describe it. The more precise the building blocks, the more complex the thoughts that can emerge... Sounds like the evolution of programming languages doesn't it?

Bookmarks

Posting Permissions

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