Solved

Use of TAB key

Posted on 1998-08-28
12
327 Views
Last Modified: 2008-02-01
I have a text editor with multiple text boxes. I would like to be able to use the TAB key to actually put tabbed spaces in the text box. I set the "tab stop" to true and false but that didn't seem to make a difference. In fact, it removes the focus from the text box if I set the tabstop to false, even though I put in if keycode = vbkeytab then text1.setfocus in the keydown event of the text box.

Any ideas?
0
Comment
Question by:cantrell
12 Comments
 
LVL 1

Expert Comment

by:jsuper
ID: 1431650
Tab stops does not insert tabs into the textbox, it just determines which controls on the form to stop at when the user presses the tab key.   You need to use special Windows API functions to insert tab positions into a text box.  Go to the website listed below and scroll down to the "textbox" topic.  Under that topic you will see a link for creating tabs in a textbox.  Good Luck.

   http://www.mvps.org/vbnet/
0
 
LVL 2

Author Comment

by:cantrell
ID: 1431651
sorry jsuper,
I checked this site out and eventually searched it but the only thing I could find was about setting Tab-Stops, not actually entering a tab into a text box. Please let me know if I missed something there.
0
 
LVL 2

Expert Comment

by:Jagar
ID: 1431652
Why don't you use the keydown event and detect for when someone presses Tab then you can just move the cursor over the specified number of characters.
0
 
LVL 2

Author Comment

by:cantrell
ID: 1431653
Because this won't move (tab) the text. It will only move the cursor. I want to be able to hit (TAB) and move the text at the current cursor position to the right 2-4 spaces (about the size of the normal tab placement).
0
 
LVL 2

Accepted Solution

by:
BergJC earned 20 total points
ID: 1431654
Could you determine where the cursor is on the textbox, then take the string that is to the right of the cursor and add 2-4 spaces to the beginning of that string. Of course, you'd then have to concantenate that new string with the string to the left of the cursor. Does that make sense?

Example:

"Test Message"
Left of cursor --> string1 = "Test Me"
Right of cursor --> string2 = "ssage"

string2 = "    " + string2
Textbox = String1 + string2

0
 
LVL 2

Expert Comment

by:BergJC
ID: 1431655
Of course, I'm thinking of putting that code in the KeyDown event, and capturing the vbTabKey.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:lawnranger
ID: 1431656
Use a RichTextBox instead of a standard one...
0
 
LVL 2

Author Comment

by:cantrell
ID: 1431657
BerjJc,
Thanks for responding.
I really don't like that as an answer (not your fault) because of the work involved in finding the cursor position, etc. There has got to be an easier way to do it. However, since I don't know what that is and evidently no-one else does - you get the points since your answer gave me an idea of at least how to do it - whether I like it or not. This is the code I used that came from the idea you had.

In the KEYDOWN event:
If KeyCode = vbKeyTab Then
txtHistory.Text = "    " & txtHistory.Text
newstart = newstart + 4
txtHistory.SelStart = txtHistory.SelStart & 4
txtHistory.SelStart = newstart
KeyCode = 0
Exit Sub
 End If
0
 
LVL 2

Author Comment

by:cantrell
ID: 1431658
BerjJc,
Thanks for responding.
I really don't like that as an answer (not your fault) because of the work involved in finding the cursor position, etc. There has got to be an easier way to do it. However, since I don't know what that is and evidently no-one else does - you get the points since your answer gave me an idea of at least how to do it - whether I like it or not. This is the code I used that came from the idea you had.

In the KEYDOWN event:
If KeyCode = vbKeyTab Then
txtHistory.Text = "    " & txtHistory.Text
newstart = newstart + 4
txtHistory.SelStart = txtHistory.SelStart & 4
txtHistory.SelStart = newstart
KeyCode = 0
Exit Sub
 End If
0
 
LVL 2

Author Comment

by:cantrell
ID: 1431659
FYI: I am using a rich text box.
0
 
LVL 1

Expert Comment

by:mraghuv
ID: 1431660
To include spaces into the text box with focus you have to use one of the following events
keydown, keypress or keyup.

you have to use the keycode or keyascii parameter of the above mentioned events.

if the keyascii value is of the tab key then

Consider Text1 as the name of the text box

Text1_KeyPress(KeyAscii)
   
      If keyAscii = 9 then
           '  If the cursor position is at the starting position of the text box then
           If Text1.SelStart = 1 then
                  Text1.Text = Space(5) & Text1.Text
                  Text1.SelStart = 6
                  Exit Sub
           ElseIf Text1.SelStart = Len(Text1.Text)
                  Text1.Text = Text1.Text & Space(5)  
                  Text1.SelStart = Len(Text1.Text)
                  Exit Sub
          Else
 
                  Text1.Text = Left(Text1.Text,Text1.SelStart) & Space(5) & Right(Text1.Text, _
                                               Len(Text1.Text) - Text1.SelStart)
                  Exit Sub
          End If    
End Sub

0
 

Expert Comment

by:gh0st
ID: 8016564
I always find this small piece of code works well

Private Sub Richtextbox1_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyTab Then
      ' add the tab space
      Richtextbox1.SelText = vbTab
      ' set KeyCode to 0 to avoid the focus being moved
      KeyCode = 0
   End If
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now