1

I write this code for tab-like behavior when user presses ENTER on textboxes for every form, which works fine.

    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
        SendKeys.Send("{TAB}")
        e.Handled = True
    End If

However, I need to write the code once, perhaps as a sub or function in a module so that I do not have to write the same for every form. I have checked various forums including Detecting Enter keypress on VB.NET and Tab Key Functionality Using Enter Key in VB.Net BUT all I get is either to write code for each textbox or for every individual form. Has anyone tried out a single code for ALL or may be several selected forms of the application? If yes, please share with me. Writing for every form still works fine for me but i need to take advantage of OOP. Thanks

Community
  • 1
  • 1
Muzoora Savior
  • 529
  • 5
  • 16

2 Answers2

0

There are many ways to implement this, one of those is to create a custom textbox User Control

Adding a control to your project is very easy just right click in your project in the solution explorer ->Add->User Control enter image description here

Give a name to your control ex. "tabedtextbox" enter image description here

Add a textbox control to your User Control enter image description here

Place the code in the keypress event of textbox1 of the UserControl

 Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        End If
    End Sub

enter image description here

Compile once in order to update the whole project with the new user control.

Goto to your form and add your new User Control, you can locate it at the Toolbox panel. enter image description here

Run your program and you will see the behavior of TAB when you press enter on each textbox.

gaby
  • 316
  • 1
  • 4
0

If You wish to allow the user to press enter in a TextBox, instead of pressing a specific button, you can use the acceptbutton property. One good way to use it, is to change the property, on the Enter and Leave events of the TextBox. That you call Click event of the Button, when User press Enter inside the TextBox.

Try this code:

Private Sub tbProject_Enter(sender As Object, e As EventArgs) Handles tbProject.Enter
    Me.AcceptButton = bSearch
End Sub
Private Sub tbProject_Leave(sender As Object, e As EventArgs) Handles tbProject.Leave
    Me.AcceptButton = Nothing
End Sub
Private Sub bSearch_Click(sender As Object, e As EventArgs) Handles bSearch.Click
    '.... actions to perfom
End Sub

Screen

Community
  • 1
  • 1
André
  • 1
  • 1