Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Aug 19, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 
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.SelStart = currSelStart + 1 
End If  

End Sub 
Deborah Kurata
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date