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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use tab in a plain text box

Status
Not open for further replies.

StrikeEagleII

Technical User
Oct 4, 2006
57
US
I have a plain text box on a form that the users can enter multiple lines of text into. I've done some searching around and it appears that you can't use the tab key to insert a tab into the field unless you use a rich text box (hassle--i was hoping you can change it's behavior like you can the enter key). To sort of simulate that, I use the keydown event of the text box to intercept the tab and cancel it and instead use sendkeys to insert four spaces:

Private Sub txtTOLSummary_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 9 And Shift = 0 Then
KeyCode = 0
SendKeys " "
End If
End Sub

That appears to work. However I am told that in windows 7 Sendkeys will be disabled and that it should not be included in code. Is there another way to do this?
Thanks,
J
 
How are ya StrikeEagleII . . .

Try this:
Code:
[blue]   Dim ctl As Control
   
   Set ctl = Me.txtTOLSummary
   
   If KeyCode = 9 And Shift = 0 Then
      KeyCode = 0
      ctl = ctl.Text & "   "
      ctl.SelStart = Len(ctl)
   End If
   
   Set ctl = Nothing[/blue]

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Unfortunately that only works if you want to insert the spaces at the end of the text you've entered. If you go back to the middle of your text and hit tab, it still appends the spaces to the end. I think I might just preface the code with a On Error Resume Next so that if windows starts throwing an error at it, it will just fail silently and the user will just have to enter the spaces the old fashioned way...Thanks for the tip though
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top