use of enter key

aditya12c

Active member
Joined
Aug 21, 2005
Messages
26
Programming Experience
Beginner
i am doing a program in which i have text box where value can be entered and after entering that value on hitting enter key the cursor should go to next textbox......... how should i do that?
 
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.
 
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.

my mistake i dint tell u what i exactly want to do... see i have 3 text box ok
but only 1st text box is visble when the form is loaded......... so user will input value in 1st text box and than if he press hit enter key that time 2nd text box should be visible..
 
It was not that I meant it was not possible... it's just that users usually expects this behaviour, and is it really worth having to deal with this extra layer for all controls involved? It will also conflict with multlines boxes.
Anyway here is an example of how it can be done, example here is for two TextBoxes on a form:
VB.NET:
'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
You can actually use this same example, and make some code to make your hardcoded "next" control visible. You see that TextBox need to AcceptReturn and how to handle it in KeyUp event. (SelectNextControl can't be used for invisible control)
 
If your first textbox is textbox1 and your invisible textbox is textbox2 then this should work :

VB.NET:
[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]

You can remove the focus call to follow JohnH's recommandation.
 
On your form set: KeyPreview = True

Copy and paste this code on any textbox data entry type form.

VB.NET:
    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
Set your tab stops in the numerical order you want to go in. This will still allow you to use enter if the textbox is multiline.
 
Back
Top