devxlogo

Generating a “Real” Tab in a Text Box

Generating a “Real” Tab in a Text Box

Here’s the code I used to trap the Tab key and use it to generate a”real” tab in a text box:

 ' Trap the tab key to allow tabs in a text box ' This function should be called by the lostFocus ' event for the control that needs to snag the tab ' Parameters: ' txtControl text box control ' Setting keyboard info Declare Function GetKeyState% Lib "User" _ (ByVal nVirtKey%) ' Virtual key values Global Const VK_TAB = &H9 Sub snagTab (txtControl As Control) Dim retVal As Integer, currSelStart As Long retVal = GetKeyState(VK_TAB) If retVal = -128 Or retVal = -127 Then ' tab key pressed currSelStart = txtControl.SelStart If currSelStart = 0 Then txtControl.Text = Chr$(9) & txtControl.Text Else txtControl.Text = Left(txtControl.Text, _ currSelStart) & Chr$(9) & _ Mid(txtControl.Text, currSelStart + 1) End If' Change the focus back to this control and ' reset the current insert point to past ' the new "tab" txtControl.SetFocus txtControl.SelStart = currSelStart + 1 End If  End Sub 
See also  How IoT Tech Will Transform Logistics
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist