SOLVED : CTRL+S on combobox

Stonkie

Well-known member
Joined
Sep 12, 2007
Messages
279
Programming Experience
1-3
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 :

VB.NET:
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 :

VB.NET:
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! :cool: 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! :D).

EDIT3: Translated the code from C# to VB.NET.
 
Last edited:
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
 
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! :rolleyes: 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...
 
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 :confused:

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...
 
This is what I'm talking about:
VB.NET:
    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
 
Alright, thanks! :)
 
VB.NET:
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
        [B]e.SuppressKeyPress = True[/B]

    End If
End Sub
 
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.
 
Back
Top