VB: RichTextBox different fonts/colors


I'm trying to do a textbox, which holds a list of names. I would like to change some of the names for example bolded.

The list will come within a message received through socket and it looks like this:

dim str1 as string
str1 = "person1 person2 person3"

And it will be worked like this:

dim str2() as string
str2 = Split(str1, " ")
str1 = Join(str2, vbCrLf)

Now I would like to print str1 to RichTextBox1, person1 bolded, person 2 underlined and person 3 plain+red (when person1 is on the list, his always bolded no matter in which order the names are...)

Any help is very appreciated! :)
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
Hi Maagrapu,

This sample should give you some idea of how to achieve your aim, it doesn't do exactly what you want but it does demonstrate how you can do it.

Private Sub Command1_Click()
    strPeople = "Person1 Person2 Person3"
    Dim aryPeople() As String
    aryPeople = Split(strPeople, " ")
    WriteToRTB aryPeople()
End Sub

Private Sub WriteToRTB(People() As String)
    Dim intPerson As Integer
    RichTextBox1.Text = Join(People, vbCrLf)
    Dim intStart As Integer
    For intPerson = LBound(People) To UBound(People)
        ColorPerson People(intPerson), intStart, intPerson
        intStart = intStart + Len(People(intPerson)) + 2
End Sub
Private Sub ColorPerson(ByVal PersonName As String, StartPos As Integer, ByVal RowNumber As Integer)
    With RichTextBox1
        .SelStart = StartPos
        .SelLength = Len(PersonName)
        Select Case RowNumber
        Case 0
            .SelBold = True
        Case 1
            .SelBold = False
            .SelUnderline = True
        Case 2
            .SelUnderline = False
            .SelColor = vbRed
        End Select
        .SelLength = 0
        .SelStart = Len(.Text)
    End With
End Sub


Brainbench MVP for Visual Basic

Experts-Exchange Advisory Board Member

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I find it easiest to type some text in another window and then copy paste it to the RTF window.  Then using the source of the RTF window, construct the box accordingly.

Private Sub Command1_Click()
Debug.Print RichTextBox1.TextRTF
End Sub

from rtf box:
{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}{\f1\fnil\fcharset0 MS Sans Serif;}}
{\colortbl ;\red255\green0\blue0;}
{\stylesheet{ Normal;}{\s1 heading 1;}{\s2 heading 2;}}
\viewkind4\uc1\pard\keepn\s1\lang1033\b\f0\fs24 Bold
\par \pard\keepn\s2\ul\b0 Underline
\par \pard\cf1\ulnone Plain red
\par \pard\cf0\f1\fs17
\par }

Create a string with the appropriate RTF tags and put it in RichTextBox1.TextRTF
Hmm, I like TimCottee's idea better :)
Hmm, I like TimCottee's idea better :)
TimCotteeHead of Software ServicesCommented:

Why the 'C' grade, as I said in my post it may not be entirely what you needed but the whole point of this site is to collaborate to produce a final solution. Your lack of response made this impossible. Please try to interact with those posting comments in your threads, it will help you get the best out of the site and will also help the experts give you a better solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.