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