?
Solved

VB: RichTextBox different fonts/colors

Posted on 2003-03-13
6
Medium Priority
?
328 Views
Last Modified: 2013-12-03
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
Comment
Question by:Maagrapu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 345 total points
ID: 8127110
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
 
LVL 1

Expert Comment

by:JamesKZO
ID: 8127123
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
 
LVL 1

Expert Comment

by:JamesKZO
ID: 8127129
Hmm, I like TimCottee's idea better :)
0
 
LVL 1

Expert Comment

by:JamesKZO
ID: 8127233
Hmm, I like TimCottee's idea better :)
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 8688758
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

762 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