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

Dani AI

Generated

Good question. The VB6 KeyAscii approach does not apply in .NET or in ASP.NET. If you are doing WinForms (as the replies from @M.Waqas Aslam and @Mitja Bonca suggest), prefer moving focus by tab order rather than targeting specific controls. That is more maintainable than keeping an array or indexing the Controls collection (which mentions). Ensure TabIndex is set correctly on each input.

WinForms: intercept Enter once at the form level and advance focus based on TabIndex. This skips disabled/hidden controls, respects nested containers, wraps at the end, and supports Shift+Enter to go backwards.

' In your form class
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
    If keyData = Keys.Enter Then
        Dim ac = Me.ActiveControl
        ' Let Enter behave normally inside multiline textboxes
        If TypeOf ac Is TextBox AndAlso DirectCast(ac, TextBox).Multiline Then
            Return MyBase.ProcessCmdKey(msg, keyData)
        End If
        Dim forward As Boolean = (ModifierKeys And Keys.Shift) = Keys.None
        Me.SelectNextControl(ac, forward, True, True, True) ' tabStopOnly, nested, wrap
        Return True ' swallow the key to avoid the system beep
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

ASP.NET Web Forms: Enter typically submits the form. If you want Enter to behave like Tab for text inputs, handle it on the client and move focus to the next tabbable element. Set TabIndex on your server controls so the order is correct, and be mindful of any DefaultButton that might also submit the form.

<script>
document.addEventListener('keydown', function (e) {
  if (e.key === 'Enter' && e.target.matches('input, select') && !e.target.matches('textarea')) {
    const form = e.target.form; if (!form) return;
    e.preventDefault();
    const focusables = Array.from(form.querySelectorAll('input, select, button, [tabindex]'))
      .filter(el => !el.disabled && el.tabIndex >= 0 && el.type !== 'hidden' && el.offsetParent !== null);
    const i = focusables.indexOf(e.target), dir = e.shiftKey ? -1 : 1, next = focusables[i + dir];
    if (next) next.focus();
  }
});
</script>

Recommended Answers

All 4 Replies

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.