I\'ve several textboxes. I would like to make the Enter button act as Tab. So that when I will be in one textbox, pressing Enter will move me to the next one. Could you plea
This worked for me
if (e.Key == Key.Enter)
((TextBox)sender).MoveFocus(new TraversalRequest(new FocusNavigationDirection()));
Here is the code that I usually use. It must be on KeyDown event.
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
UPDATE
Other way is sending "TAB" key! And overriding the method make it so easier :)
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Enter))
{
SendKeys.Send("{TAB}");
}
return base.ProcessCmdKey(ref msg, keyData);
}
I would combine what Pharabus and arul answered like this:
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ‘\r’)
{
e.Handled = true;
parentForm.GetNextControl().Focus()
}
}
Let me know if this helps! JFV
It is important to note that if you will get an annoying "ding" or warning sound each time that the control is expecting an associated button control and e.Handled = true
isn't always the answer to rid yourself of the noise/sound/ding.
If the control (i.e. single-line textbox) is expecting an associated button to 'ENTER' your entry, then you must also deal with the 'missing' control.
e.Handled = e.SuppressKeyPress = true;
This may be helpful when getting rid of the 'ding' sound.
For what it's worth- in my circumstance my users needed to use the "ENTER KEY" as we were transitioning from a terminal/green-screen application to a windows app and they were more used to "ENTER-ing" through fields rather than tabbing.
All these methods worked but still kept the annoying sound until I added e.SuppressKeyPress
.
I use this code in one of the text box keydown event
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
Unable handle this keydown
event for all text boxes in my form. Suggest something. Thanks
If you define Tab Order of all controls and make Form.KeyPreview = True, only need this:
Private Sub frmStart_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
System.Windows.Forms.SendKeys.Send("{TAB}")
End If
End Sub