Solved

Use of TAB key

Posted on 1998-08-28
12
326 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Of course, I'm thinking of putting that code in the KeyDown event, and capturing the vbTabKey.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Expert Comment

by:lawnranger
Comment Utility
Use a RichTextBox instead of a standard one...
0
 
LVL 2

Author Comment

by:cantrell
Comment Utility
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
Comment Utility
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
Comment Utility
FYI: I am using a rich text box.
0
 
LVL 1

Expert Comment

by:mraghuv
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 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

10 Experts available now in Live!

Get 1:1 Help Now