Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

RichTextBox and multiline Tab

Posted on 2005-04-08
7
Medium Priority
?
781 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:doinVBstuff
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:Shauli
ID: 13744033
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
 

Author Comment

by:doinVBstuff
ID: 13745988
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
 
LVL 22

Expert Comment

by:danaseaman
ID: 13746640
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:doinVBstuff
ID: 13749621
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
 
LVL 22

Accepted Solution

by:
danaseaman earned 500 total points
ID: 13749955
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
 

Author Comment

by:doinVBstuff
ID: 13750050
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
 
LVL 22

Expert Comment

by:danaseaman
ID: 13750091
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

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
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…

579 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