Using return key to move through fields

larris

Member
Joined
Mar 13, 2006
Messages
23
Programming Experience
1-3
hi guys

I have a form and i need to navigate to the next field not only with the tab key but olso with the return key.Is there an easy way to do it ??
 
something like this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/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.EventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].KeyPreview = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_KeyUp([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].KeyUp
[/SIZE][SIZE=2][COLOR=#0000ff] If[/COLOR][/SIZE][SIZE=2] e.KeyCode = Keys.Enter [/SIZE][SIZE=2][COLOR=#0000ff]Then [/COLOR][/SIZE][SIZE=2]getnextTab()[/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] getnextTab()
[/SIZE][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] c [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control =  [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].GetNextControl(ActiveControl, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]  If[/COLOR][/SIZE][SIZE=2] c [/SIZE][SIZE=2][COLOR=#0000ff]Is [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] c = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].GetNextControl([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
  ActiveControl = c[/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Also you can do that as it follows:
VB.NET:
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] NextControl_KeyDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#008000][COLOR=blue][SIZE=2]
[/SIZE][/COLOR][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   If[/COLOR][/SIZE][SIZE=2] e.KeyCode = Keys.Enter [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]      SendKeys.Send([/SIZE][SIZE=2][COLOR=#800000]"{TAB}"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]   End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

then add handler itterating through collection controls of certain form .. like:
VB.NET:
[COLOR=blue]For each[/COLOR] xControl [COLOR=blue]as[/COLOR] Control [COLOR=blue]in[/COLOR] [COLOR=blue]Me[/COLOR].Controls
  [COLOR=blue]If[/COLOR] xControl.Gettype [COLOR=blue]Is[/COLOR] [COLOR=blue]getType[/COLOR](TextBox) [COLOR=blue]then[/COLOR]
      [COLOR=blue]addhandler[/COLOR] xControl.KeyDown, [COLOR=blue]addressof[/COLOR] NextControl_KeyDown
  [COLOR=blue]End if[/COLOR]
[COLOR=blue]Next[/COLOR]


Regards ;)
 
Why is it that it "beeps" everytime the return key is pressed. Is it trying to validate the information inside the objects?
 
It's the standard Windows sound signal (Control Panel, Sounds). You have to set e.Handled=True in KeyPress event to suppress it.
 
I tried that out and it gets rid of the sound, but not the form will not allow me to enter anything into my textboxes. Why is that? I am just trying to get an understanding of why. The user who will be working with my application is not allowed to have speakers so I don't think the sound will be any problem at all.

Did you mean the keypress event of the objects and not the form?
 
I meant the forms KeyPress event, but you have to do a trick using a boolean value between the Key events, for example check for Enter key in KeyDown and if so you set the boolean, when Keypress happens after the KeyDown you set e.Handled = the boolean then resets boolean. If you find an easier path just post it.
 
Keypress without beep to move to next control

You could place this code in each textbox's Keypress event:


Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
SendKeys.Send("{TAB}")
e.Handled =
True 'stops beep
End If
End Sub


The following code moves focus to a specific control, which doesn't have to be in the same Tab order:


Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar = Chr(13) Then
TextBox3.Focus()
e.Handled =
True 'stops beep
End If
End Sub

 
Solitaire, I believe that was about what Kulrom posted #3.
 
But I used the KeyPress event, not the KeyDown event.

I tried using e.handled = True with the KeyDown event and it doesn't suppress the beep. It does in the KeyPress event. The OP didn't want the beep.
 
Yes, that is true, that's what I said in post 6 also.
 
I meant the forms KeyPress event, but you have to do a trick using a boolean value between the Key events, for example check for Enter key in KeyDown and if so you set the boolean, when Keypress happens after the KeyDown you set e.Handled = the boolean then resets boolean. If you find an easier path just post it.

Thx for the help! Here is what I have added to my code.(For the next guy.)



VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form2_KeyDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].KeyDown[/SIZE]
[SIZE=2][COLOR=#0000ff]   If[/COLOR][/SIZE][SIZE=2] e.KeyCode = Keys.Enter [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] keyalter = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form2_KeyPress([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/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][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].KeyPress[/SIZE]
[SIZE=2]   e.Handled = keyalter[/SIZE]
[SIZE=2]   keyalter = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
You can also combine the other suggestions and only use the keypress event with this code:
VB.NET:
If e.KeyChar = Chr(Keys.Enter) Then
 
Back
Top