Solved

RichTextBox Stuff

Posted on 2003-11-02
9
504 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

743 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

14 Experts available now in Live!

Get 1:1 Help Now