Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having Enter key Function as Tab Key 1

Status
Not open for further replies.

bigmelon

MIS
Sep 25, 2003
114
US
Does anyone know how to have the enter key function as a tab key on forms. I know you can do it individually for each textbox and on keypress set the focus to the next textbox, but I am looking for a simple way to do it for a whole form. Also, is it possible to do this within a datagrid?

Thanks a million,

Jeremy
 
My approach was to enable KeyPreview on the parent form, then in the event handler for KeyDown for the parent form, I would check Me.ActiveControl to make sure it was a TextBox, ComboBox, etc.

Then, use Me.SelectNextControl to move forward in the tab sequence.

Check out
 
could you post a sample of your code for the keydown event? I am not familiar with the activecontrol and selectnextcontrols.

Thanks,

Jeremy
 
KeyPreview is available in the design time form property page in the IDE. Just hook up to the KeyDown event for the form using the IDE like you normally would. Then:

Code:
Private Sub Form2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
If (e.KeyCode = Keys.Enter And Me.ActiveControl Is TextBox) Then
   Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
End If
End Sub

Its been awhile since I've written VB code. I apologize if the syntax is wrong.
 
It highlights TextBox and says 'Textbox' is a type and cannot be used as an expression. If I just skip the if altogether it gets hungup on the comboboxes. Any ideas?

Thanks a million,

Jeremy
 
Shoot, can't remember how to do type comparisons. Does this work?

GetType(Me.ActiveControl) = TextBox
 
That is the correct format, but it still will not work for my comboboxes even if i changes the gettype to combobox?

Thanks again
 
You may need additional code to make .NET happy:

Code:
Private Sub Form2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
If (e.KeyCode = Keys.Enter And (Me.ActiveControl.GetType() = GetType(TextBox) Or Me.ActiveControl.GetType() = GetType(ComboBox))) Then
   Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
   e.Handled = True
End If
End Sub

You may also want to put a break point on the If statement and make certain that the event is being called for your ComboBoxes. If the statement is being called, verify that Me.ActiveControl is returning the ComboBox by using the Locals window, that the type of the class is ComboBox, and then step forward in your code using F10 to make sure you enter the If statement.
 
The event isn't being called for my comboboxes?? What would cause this?
 
From what I am seeing on the net this is a MS issue. Is there any chance you can change those ComboBoxes to DropDownLists? The event should fire then.
 
No I can't do that, I am just going to have to hard code those ones using the focus i think. My only other question is you don't have any idea how I could make this work within a datagrid do you?

Thanks a lot. You have been a huge help.

Jeremy
 
I found this code that is supposed to work to make the enter function as a tab:

Public Class MyDataGrid

Inherits DataGrid

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean

If msg.WParam.ToInt32() = CInt(Keys.Enter) Then

SendKeys.Send("{Tab}")

Return True

End If

Return MyBase.ProcessCmdKey(msg, keyData)

End Function 'ProcessCmdKey

End Class 'MyDataGrid

But I can't figure out how to call it in the keypress or keydown events. Does anyone know how I can make this work?

Thanks in advance

Jeremy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top