Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

VB: RichTextBox different fonts/colors

Hi!

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! :)
0
Maagrapu
Asked:
Maagrapu
  • 3
  • 2
1 Solution
 
TimCotteeCommented:
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
    Next
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

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
 
JamesKZOCommented:
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
0
 
JamesKZOCommented:
Hmm, I like TimCottee's idea better :)
0
 
JamesKZOCommented:
Hmm, I like TimCottee's idea better :)
0
 
TimCotteeCommented:
Maagrapu:

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.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now