I am trying to use the mouse as little as possible and for this I need to know if there is any way to be able to change control when pressing the key Enter
.
The idea I have is that if I'm on a form with several controls, I'm mostly using controls of type TextBox
but I could have DateTimePickers
or ComboBox
; that when pressing Enter
skip to the next control, using the order that is given to them when creating them in the form with the property TabIndex
.
private void txtOrdenServicio_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
Cargar_OrdenesServicio();
txtRazonTrabajo.Focus();
}
}
With the previous code I have put it in one of the events KeyPress
but if I have 10 I TextBox
would have to place the control that follows manually. I am working with Windows Form.
Is there any way to achieve this jump?
To pass to the other control you just need to handle the event
OnKeydown
of the control you want to work with.Example:
Or example 2:
If your method
Cargar_OrdenesServicio();
performs some type of action on the control when changing its value in the text, I recommend you to put said call inside the eventOnTextChanged
.In VB.NET what you want would be a bit easier to implement, because you can handle different unsigned events from the same method, but C# doesn't support implementing an event unless it has its required signature, for events of type
Key
, the required signature bears the following:That said, you can implement the same method on all controls that support the event
OnKeyDown
from the event browser:Signatures and Overloads if you want to understand a little more than what I mentioned.
I hope it has helped you.
The key is to use the GetNextControl () with this you will be able to know which is the next control that should take the focus.
Using Enter Key as a Tab
as you will see the keypress you define it at the level of the form to make it global
You could also consider using the Form.ProcessCmdKey to control the form keys even more generally.
For it to work correctly it is important that you use the PreviewKeyDown event
'''
'''