JohnH said:You would rather want to accept the default behaviour of the Windows operating system, where TAB is the key to move through fields and controls. All input controls have default properties for this, speaking of the .TabStop and .TabIndex properties.
'standard (sender, e) parameters shortened
Private Sub Form1_Load(sender, e) Handles MyBase.Load
TextBox1.AcceptsReturn = True
AddHandler TextBox1.KeyUp, AddressOf Control_EnterNext
TextBox2.AcceptsReturn = True
AddHandler TextBox2.KeyUp, AddressOf Control_EnterNext
End Sub
Private Sub Control_EnterNext(sender, e)
If e.KeyCode = Keys.Enter Then
e.Handled = True
Me.SelectNextControl(sender, True, True, True, True)
End If
End Sub
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TextBox1_KeyPress([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyPressEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TextBox1.KeyPress[/SIZE][INDENT][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] e.KeyChar = ControlChars.Cr [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][INDENT]TextBox2.Visible = [/INDENT][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]TextBox2.Focus()
[/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
Private Sub Form1_KeyPress _
(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles MyBase.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
If TypeOf Me.ActiveControl Is TextBox Then
Dim tb As TextBox = DirectCast(Me.ActiveControl, TextBox)
If tb.Multiline AndAlso tb.AcceptsReturn Then
e.Handled = False
Exit Sub
End If
End If
e.Handled = True
Dim oform As Form = Me.FindForm
oform.SelectNextControl(oform.ActiveControl, True, True, True, True)
oform.ActiveControl.Focus()
End If
End Sub