Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

This should be simple, but I can not figure it out.

 

I have about 100 textbox on a form. The users of this form will be use to having the "enter" key to move on to the next item. The "tab" key performs this function fine, but the users will complain if they do not have the "enter" key.

 

Can anyone help me with simple code that would be independent of the textbox, that when the "enter" key is pressed, it performs the function of the tab key. I have been able to get it to work for an individual textbox, but I don't want to repeat the code 100 times.

Posted

First you need to set the form's KeyPreview property to True. Then you can easily catch all keys being pressed in the OnKeyDown event and evaluate them.

 

Here's the code that changes the focus to the next control when the user hits Enter from within a TextBox;

 

Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
   Dim ctl As Control = Me.ActiveControl

   If TypeOf ctl Is TextBox And e.KeyValue = Keys.Enter Then
       Me.SelectNextControl(ctl, True, True, True, True)
       e.Handled = True
   End If
End Sub

Gamer extraordinaire. Programmer wannabe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...