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
For those of you that code in vb...
Public Class NoReturnTextBox
Inherits System.Windows.Forms.TextBox
Const CARRIAGE_RETURN As Char = Chr(13)
' Trap for return key....
Private Sub NoReturnTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = CARRIAGE_RETURN Then
e.Handled = True
System.Windows.Forms.SendKeys.Send(vbTab)
End If
End Sub
End Class
You can write on the keyDown of any control:
if (e.KeyCode == Keys.Enter)
{
if (this.GetNextControl(ActiveControl, true) != null)
{
e.Handled = true;
this.GetNextControl(ActiveControl, true).Focus();
}
}
GetNextControl doesn't work on Vista.
To make it work with Vista you will need to use the code below to replace the this.GetNextControl...:
System.Windows.Forms.SendKeys.Send("{TAB}");
I don't do it at the form level. I create a single method that I share across all my inputs KeyDown event that I want to do this with (with one exception):
private void alltextBoxes_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
}
catch
{
}
}
If I'm writing a control that I want to use in other applications, I give the last input its own method like this:
private void lastinput_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
System.Windows.Forms.SendKeys.Send("{TAB}");
}
}
catch
{
}
}
Otherwise, the control just loops inside itself when you try to run it in another project. You could use the second way everywhere but I think the 1st is the preferred way.
This is the solution I use for VB.NET
Set Keypreview=True in your form properties.
Put this code in form keydown event:
If (e.KeyData = Keys.Enter) Then
'for any multiline control, you have to exit to let multiline 'textbox intro 'keypressing makes line skips.
If ActiveControl.Name = txtMyMutilineTextBox.Name Then Exit Sub
e.SuppressKeyPress = True
SelectNextControl(ActiveControl, True, True, True, True)
End If
Enjoy !!!!
Xabier Aberasturi Larruzea
You don't need to make an "enter event handler"
All you need to do is make a "central" KeyDown event:
example
private void General_KeyDown(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (this.GetNextControl(ActiveControl, true) != null)
{
e.Handled = true;
this.GetNextControl(ActiveControl, true).Focus();
}
}
}
Then all you have to do is go to designer select all textboxes you wish to cycle through with EnterKey (select them by holding down Ctrl and clicking on textbox with the mouse) then go to Events(thunder like button), search Keydown event and type inside General_KeyDown. Now all your selected Textboxes will have the same keydown event :) This makes everything muuuuch much easier, cause imagine a form with 100 textboxes and you want to cycle through all with enter.... making an apart event for each texbox is... well not a proper way to make a program, it ain't neat. Hope it helped!!
Blockquote
Taking a wild guess:
// on enter event handler
parentForm.GetNextControl().Focus();