Solved

RichTextBox Stuff

Posted on 2003-11-02
9
517 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 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

12 Experts available now in Live!

Get 1:1 Help Now