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 :
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?
If e.Control AndAlso e.KeyCode = Keys.S Then
e.Handled = True
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 :
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.
If e.KeyChar = (Char)(Keys.S And Keys.Control) Then
e.Handled = True
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 4: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?