Solved

RichTextBox Stuff

Posted on 2003-11-02
9
545 Views
Last Modified: 2008-03-06
Hello, I found this on the internet a while back, I know that there is a way to actually get it so I can check it to "Public Sub ChangeColorText" and have it serach for text within a richbox and change it to a diffrent color.  I would use instr but not sure how to get it worked out in this one..

Please let us know.

Public Sub AddColorText(RTB As RichTextBox, Color As Long, Text As String)
    Dim R As Long
    Dim G As Long
    Dim B As Long
   
    RTB.SelStart = Len(RTB.Text)
    R = Color Mod &H100
    G = (Color \ &H100) Mod &H100
    B = (Color \ &H10000) Mod &H10000
    RTB.SelRTF = "{{\colortbl;\red" & CStr(R) & "\green" & CStr(G) & "\blue" & CStr(B) & ";}" & "{\cf1 " & Text & "}}"
End Sub
0
Comment
Question by:wd006451
  • 6
  • 3
9 Comments
 
LVL 1

Author Comment

by:wd006451
ID: 9666264
An example of what I want to do is lets say i have the following in a string:

dim ABXX as string
ABXX ="This is sample text" & vbcrlf & _
           "{Start of internal notes}" & vbcrlf & _
           "This is internal notes" & vbcrlf & _
           "{End of internal notes}" & vbcrlf  & _
           "More sample notes"

richtextbox1=ABXX


I want to replace start of internl notes to the end of the internal notes (including the specifications), i want to replace it to red text.
0
 
LVL 1

Author Comment

by:wd006451
ID: 9666993
Ok, here is what I have so far, and I still can't figure it out, this code works great, but only for the first match found.

Private Sub Command1_Click()
ChangeColorInWord RichTextBox1, vbBlue, Text1
End Sub

Public Sub ChangeColorInWord(RTB As RichTextBox, Color As Long, FindString As String)
    Dim R As Long
    Dim G As Long
    Dim B As Long
    Dim FinInx
    Dim FinInx1
    Dim XFindString
    Dim BBx
    BBx = 0
    BBx = BBx + Len(FindString)
    FinInx = RTB.Find(FindString, BBx, Len(RTB.TextRTF), rtfMatchCase)
    Do
        If FinInx < 0 Then Exit Do
        If Mid(RTB.Text, FinInx, 1) <> vbCrLf And Mid(RTB.Text, FinInx, 1) <> " " Then
            FindString = Mid(RTB.Text, FinInx, 1) + FindString
            FinInx = FinInx - 1
        Else
            Exit Do
        End If
    Loop
    XFindString = ""
    FinInx1 = FinInx + BBx
    XFindString = FindString
           
    Do
        If FinInx1 > Len(RTB.Text) Then Exit Do
        If Mid(RTB.Text, Len(XFindString) + FinInx1, 1) <> vbCrLf And Mid(RTB.Text, Len(XFindString) + FinInx1, 1) <> " " Then
            FindString = FindString + Mid(RTB.Text, Len(XFindString) + FinInx1, 1)
            FinInx1 = FinInx1 + 1
        Else
            Exit Do
        End If
    Loop
           
    RTB.SelStart = FinInx
    RTB.SelLength = Len(FindString)
    R = Color Mod &H100
    G = (Color \ &H100) Mod &H100
    B = (Color \ &H10000) Mod &H10000
    RTB.SelRTF = "{{\colortbl;\red" & CStr(R) & "\green" & CStr(G) & "\blue" & CStr(B) & ";}" & "{\cf1 " & FindString & "}}"
End Sub
0
 
LVL 1

Accepted Solution

by:
rashmitodkar earned 130 total points
ID: 9669403
hay hi
   u can try the following code i hope it will help u.


Private Sub Form_Load()
RichTextBox1.Text = "This is sample text" & vbCrLf & _
           "{Start of internal notes}" & vbCrLf & _
           "This is internal notes" & vbCrLf & _
           "{End of internal notes}" & vbCrLf & _
           "More sample notes"

AddColorText RichTextBox1, &HFF0000, "{"
End Sub

Public Sub AddColorText(RTB As RichTextBox, Color As Long, Text As String)
cnt = 1
sArr = Split(RichTextBox1.Text, Text)
For i = 0 To UBound(sArr) - 1
    RTB.SelStart = InStr(cnt, RTB.Text, Text) - 1
    RTB.SelLength = InStr(cnt, RTB.Text, "}") - InStr(cnt, RTB.Text, Text) + 1
    RTB.SelColor = Color
    cnt = Len(Text) + InStr(cnt, RTB.Text, "}")
Next
RTB.SelStart = Len(RTB.Text)
RTB.SelLength = 0
End Sub

0
Independent Software Vendors: 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 1

Author Comment

by:wd006451
ID: 9670209
Thank you rashmitodkar

I have increased the points to 130, can you show me how to do the same with hyperlinks and email addresses?

I am asuming that i would have to search the whole word with the strings "@" "//" etc.... this would make the whole word stopped by spaces and vbcrlfs etc... to be blue.
0
 
LVL 1

Expert Comment

by:rashmitodkar
ID: 9670373
can u please make this more clear
that what exactly u want to do
0
 
LVL 1

Author Comment

by:wd006451
ID: 9670575
I want a whole word that has the strings "@" or "//" in them to also be a specifc color, (it would be  diffrent function all togather.

Hope that makes sence..
0
 
LVL 1

Author Comment

by:wd006451
ID: 9672221
Also, what can i do for the background?
0
 
LVL 1

Expert Comment

by:rashmitodkar
ID: 9676265
first u just take an array of all words using split function and having split by space.
and then for each item in array u can use instr to find the char and then in the same way u can change its color
using selcolor.
0
 
LVL 1

Author Comment

by:wd006451
ID: 9678976
I tried that, but cant get it to work, can you show me some code?
0

Featured Post

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!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

685 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