Solved

Use of TAB key

Posted on 1998-08-28
12
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 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