?
Solved

RichTextBox Stuff

Posted on 2003-11-02
9
Medium Priority
?
563 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
[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
  • 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 520 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
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!

 
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

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.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

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