RichTextBox and multiline Tab

You know in the VB6 Code section of any project you're working on, you can select multiple lines of text. This is usually quick and simple. Now, if you select multiple lines of text, you can press the Tab key and ALL of the lines will add tabs in front of them. So, for instance, you have this as code:

Public Function CheckMeOut()
Dim ThisOne as String
ThisOne = "Coolness!"
Debug.Print ThisOne
End Function

You can select the middle part of it (All 3 lines, simultaneously), press Tab and it moves over like this:

Public Function CheckMeOut()
    Dim ThisOne as String
    ThisOne = "Coolness!"
    Debug.Print ThisOne
End Function

I need this functionality in a RichTextBox. What is the easiest way to achieve this without parsing the information and adding the Tabs behind the Line Breaks (Carriage returns/Line Feeds or VBCrLF's)? Is there some sort of API or setting that will allow me to do this with ease?

Code is appreciated. Thanks for your responses.
doinVBstuffAsked:
Who is Participating?
 
danaseamanCommented:
The VbCrLf appears as a tiny space following last char of line and can be selected or not along with the text. It is quite easy as you have noticed to accidentally include it in the selection.

This simple modification to the code I provided should solve this issue and should work whether you select the trailing vbCrLf or not.

Private Sub cmdInsert_Click()
   Dim i As Long
   Dim s As String
   Dim v() As String
   s = RichTextBox1.SelText
   v = Split(s, vbCrLf)
   For i = 0 To UBound(v)
      If LenB(v(i)) Then
         v(i) = vbTab & v(i)
      End If
   Next
   s = Join(v, vbCrLf)
   RichTextBox1.SelText = s
End Sub
0
 
ShauliCommented:
Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
If KeyAscii = 9 Then
    KeyAscii = 0
    RichTextBox1.SelIndent = RichTextBox1.SelIndent + 240
End If
End Sub

'you can modify the '240' to any value you want. It will determine how much to indent.

S
0
 
doinVBstuffAuthor Commented:
Sorry, I guess I should have clarified. I need for the text to have Tabs in front, not a change of indent.

See, the text I would save in the RTB would be rtfText and not rtfRTF, hence the need for the insertion of tabs. Indentation only provides this for the period I am working on the item. When I save it as rtfText, all formatting is lost, which is how I want it. So, to make the difference (within my program or anything, like Notepad), I would need to have the Tabs inserted, not indents.

Thanks for trying though.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
danaseamanCommented:
I don't think you will find an API that does what you want but the code to do this is quite simple. Select the lines and execute this code executed from command button cmdInsert:
Private Sub cmdInsert_Click()
   Dim i As Long
   Dim s As String
   Dim v() As String
   s = RichTextBox1.SelText
   v = Split(s, vbCrLf)
   For i = 0 To UBound(v)
      v(i) = vbTab & v(i)
   Next
   s = Join(v, vbCrLf)
   RichTextBox1.SelText = s
End Sub
0
 
doinVBstuffAuthor Commented:
That works pretty good. I need to work with it some. See, if you select 3 lines, RTB's have a tendency of selecting the last vbCrLf. When that happens, the line below the selected lines gets a Tab added too.

Also, an issue with the RTB unselecting the selected area. But that should be simple.

Let me work with these and I will let you know how it goes. Thanks for the sample code also.
0
 
doinVBstuffAuthor Commented:
Much quicker than the solution I was just working with! I was using string comparatives and changing the For loop by -1 if needed. But this way is MUCH more effective.

I figured out the issue with the selection. Just used a lastPos array to hold the info and shot it back at the end:

rtb.SelStart = lastPos
rtb.SelLength = len(s)

And that worked out good as well. Many thanks! The points are yours!

(I GOTTA remember about the positive benefits of Split and Join! :-) )
0
 
danaseamanCommented:
Thanks for the points.
Additional code snippet to remove the Tabs from selected text:

Private Sub cmdRemove_Click()
   Dim i As Long
   Dim s As String
   Dim v() As String
   s = RichTextBox1.SelText
   v = Split(s, vbCrLf)
   For i = 0 To UBound(v)
      v(i) = Replace$(v(i), vbTab, vbNullString)
   Next
   s = Join(v, vbCrLf)
   RichTextBox1.SelText = s
End Sub

Instead of buttons you could also run the code from a HotKey(F1 or F2 for example) using this code:

Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
   If Len(RichTextBox1.SelText) Then
      Select Case KeyCode
         Case 112 'F1
            cmdInsert_Click
         Case 113 'F2
            cmdRemove_Click
      End Select
   End If
End Sub

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.