Solved

RichTextBox Stuff

Posted on 2003-11-02
9
524 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
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 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…
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…

816 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

8 Experts available now in Live!

Get 1:1 Help Now