I want to format my textboxes in such a way that when someone fills in the details in the textbox if they press enter button on the keybord the focus is given to the next textbox that they should fill in .I tried the code below that we used in vb6 but its not working. can anyone help

If keyascii = 13 Then

        End If

ok lets assume if we have two textboxes , txt1 and txt2 , i want to get control on txt2 if i press enter and also got control txt1 if i press enter in txt2 , use this code

'---use this code at the keypress event of the control .
if e.keychar = chr(keys.enter) then 
'write the name of your control where you want to get focus like this 
txt2.focus() 
end if

regards

I did an example code how you can itterate through textBoxes on enter key press.
NOTE: my example uses only 3 textbox controls. If your have more, please enter all of them into textBox array!!

Code:

Public Partial Class Form1
	Inherits Form
	Private tbs As TextBox()
	Public Sub New()
		InitializeComponent()
		tbs = New TextBox() {textBox1, textBox2, textBox3}
		For i As Integer = 0 To tbs.Length - 1
			tbs(i).Tag = i
			tbs(i).KeyDown += New KeyEventHandler(AddressOf textBoxes_KeyDown)
		Next
		tbs(0).Focus()
	End Sub

	Private Sub textBoxes_KeyDown(sender As Object, e As KeyEventArgs)
		Dim tb As TextBox = TryCast(sender, TextBox)
		If tb IsNot Nothing Then
			If e.KeyCode = Keys.Enter Then
				Dim tag As Integer = CInt(tb.Tag)
				If tag = 2 Then
					tbs(0).Focus()
				Else
					tbs(tag + 1).Focus()
				End If
			End If
		End If
	End Sub
End Class

What you could also do is:

'Set the indexes of the controls, then use this code:


'Capture the enter key and go to next control

Where i is the index of the control
 Me.Controls(i).Focus()

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
MsgBox "hi"
End If

End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.