rich text box

i wnat my RTB to show more than one color and more than 1 font at the same time in the same box
i know that its possible but how
isnt there any scripets like (FB!) that would make your font bold, also i have to change color
LVL 2
Tai-SanAsked:
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 Tai-San,

You can manipulate the contents of the RTB using the .Sel**** properties:

With RichTextBox1
  .SelStart = 5
  .SelLength = 5
  .SelColor = vbBlue
  .SelFontName = "Courier New"
  .SelBold = True
End With

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
Tai-SanAuthor Commented:
1 prob
it doesnt work

Private Sub Form_Load()
With TXTRTF
  .SelStart = 0
  .SelLength = 0
  .SelColor = vbBlue
  .SelFontName = "Courier New"
  .SelBold = True
End With
TXTRTF.Text = "hi" & vbCrLf
With TXTRTF
  .SelStart = 0
  .SelLength = 0
  .SelColor = vbRed
  .SelFontName = "Courier New"
  .SelBold = True
End With
TXTRTF.Text = TXTRTF.Text & "hi"
End Sub

this doesnt work
0
TimCotteeHead of Software ServicesCommented:
Because you are setting .SelLength to 0 each time.

Private Sub Form_Load()
    With TXTRTF
        .Text = "hi" & vbCrLf
        .SelStart = 0
        .SelLength = 2
        .SelColor = vbBlue
        .SelFontName = "Courier New"
        .SelBold = True
        .Text = .Text & "hi"
        .SelStart = 4
        .SelLength = 2
        .SelColor = vbRed
        .SelFontName = "Courier New"
        .SelBold = True
        .SelStart = Len(.Text)
        .SelLength = 0
    End With
End Sub

Will give you a blue "hi" then on the next line a red "hi"
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tai-SanAuthor Commented:
Private Sub RichTextBox2_KeyPress(KeyAscii As Integer)
Dim Bef As String
If KeyAscii = 13 Then
    With RichTextBox1
        Bef = Len(.Text)
        .Text = .Text & "Zhasha says:" & vbCrLf
        .SelColor = vbBlack
        .SelFontName = "Times New Roman"
        .SelBold = False
        .SelFontSize = 10
        .SelStart = Len(Bef)
        .SelLength = Len(RichTextBox2)
        Bef = Len(.Text)
        .Text = .Text & RichTextBox2.Text & vbCrLf & vbCrLf
        .SelColor = vbRed
        .SelFontName = "Courier New"
        .SelBold = True
        .SelFontSize = 8
        .SelStart = Len(Bef)
        .SelLength = Len(RichTextBox2)
    End With
    RichTextBox2.Text = ""
    KeyAscii = 0
End If
End Sub

whats wrong here, its supposted to add new text every time you press enter and so it does but now with the right fonts and color :(
0
Tai-SanAuthor Commented:
okey now i got this far:

Private Sub RichTextBox2_KeyPress(KeyAscii As Integer)
Dim Bef As String
If KeyAscii = 13 Then
    With RichTextBox1
        Bef = Len(.Text)
        .Text = .Text & "Zhasha says:" & vbCrLf
        .SelStart = Bef
        .SelLength = Len(RichTextBox2)
        .SelColor = vbBlack
        .SelFontName = "Tahoma"
        .SelBold = False
        .SelFontSize = 10
        Bef = Len(.Text)
        .Text = .Text & RichTextBox2.Text & vbCrLf & vbCrLf
        .SelStart = Bef
        .SelLength = Len(RichTextBox2)
        .SelColor = vbRed
        .SelFontName = "Courier New"
        .SelBold = True
        .SelFontSize = 8
        .SelStart = Len(.Text)
        .SelLength = 0
    End With
    RichTextBox2.Text = ""
    KeyAscii = 0
End If
End Sub

but this removes all color from previous entered text, is there any way to preserve color and font from previous lines
0
Tai-SanAuthor Commented:
Private Sub RichTextBox2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
    With RichTextBox1
       
        Bef = Len(.Text)
        .Text = .Text & IName & " says:" & vbCrLf
        .SelStart = Bef
        .SelLength = Len(IName & " says:")
        .SelColor = vbBlack
        .SelFontName = "MS Sans Serif"
        .SelBold = False
        .SelFontSize = 10
       
        Bef = Len(.Text)
        .Text = .Text & RichTextBox2.Text & vbCrLf & vbCrLf
        .SelStart = Bef
        .SelLength = Len(RichTextBox2)
        .SelColor = vbRed
        .SelFontName = "Courier New"
        .SelBold = True
        .SelFontSize = 8
        .SelStart = Len(.Text)
        .SelLength = 0
    End With
    RichTextBox2.Text = ""
    KeyAscii = 0
    kk = 0
End If
End Sub

this drives me nuts IT WONT WORK
if any of you have ever used messenger then you know what i mean

its supposted to show:

vbBlack: Zhasha says:
vbRed: Hello

vbBlack: Zhasha says:
vbRed: hey

but it shows:

vbBlack: Zhasha says:
vbBlack: Hello

vbBlack: Zhasha says:
vbRed: hey
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Create a new project and add one richtextbox and two textboxes.  Paste the code below and type into each of the boxes and hit enter.

You can't use this type of construction to add things to a richtextbox:

.Text = .Text & IName & " says:" & vbCrLf

The .text property returns the text of the richtextbox but strips the encoding.

Regards,

Idle_Mind

' ******************** Code Follows ********************
Private Sub Form_Load()
    RichTextBox1.Text = ""
    Text1.Text = ""
    Text2.Text = ""
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        With RichTextBox1
            .SelStart = Len(.Text)
            .SelLength = 0
            .SelColor = vbBlack
            .SelFontName = "MS Sans Serif"
            .SelBold = False
            .SelFontSize = 10
            .SelText = "Person1 says: "
            .SelColor = vbRed
            .SelText = Text1.Text & vbCrLf
        End With
        Text1.Text = ""
        KeyAscii = 0
    End If
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        With RichTextBox1
            .SelStart = Len(.Text)
            .SelLength = 0
            .SelColor = vbBlack
            .SelFontName = "MS Sans Serif"
            .SelBold = False
            .SelFontSize = 10
            .SelText = "Person1 says: "
            .SelColor = vbBlue
            .SelText = Text2.Text & vbCrLf
        End With
        Text2.Text = ""
        KeyAscii = 0
    End If
End Sub
0

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
Mike TomlinsonMiddle School Assistant TeacherCommented:
Did you try my code Tai-San?
0
rekhasriCommented:
hi tai-san,

I presume u cannot modify ur design with 2 text boxes as suggested by idle_mind and it looks as if, the formatting is lost when u concatenate the rtb contents.

so here is some code, which puts ur content in the red text first, then looks for the "Zasha Says:" and changes the color.. looks like a round-about method, but if u have no option, u cud use this.

Private Sub rtb2_KeyPress(KeyAscii As Integer)
    Dim iLen    As Integer
    Dim istart  As Integer
    Dim iPos    As Integer
    Dim sVal    As String
    If KeyAscii = 13 Then   'if enter key then
        With rtb1           ' set red font for all text
            .Text = Trim(.Text) & "Zasha Says:" & vbCrLf & rtb2.Text & vbCrLf & vbCrLf
            .SelStart = 0
            .SelLength = Len(.Text)
            .SelColor = vbRed
            .SelFontName = "Courier New"
            .SelBold = True
            .SelFontSize = 8
        End With
        'search for "Zasha Says:" and change the color to black
        istart = 1
        iPos = InStr(1, rtb1.Text, "Zasha Says:", vbBinaryCompare)
        While iPos <> 0
            With rtb1
                .SelStart = iPos - 1
                .SelLength = Len("Zasha Says:")
                .SelColor = vbBlack
                .SelFontName = "Tahoma"
                .SelBold = False
                .SelFontSize = 10
            End With
            istart = istart + Len("Zasha Says:")
            iPos = InStr(istart, rtb1.Text, "Zasha Says:", vbBinaryCompare)
        Wend
        rtb2.Text = vbNullString
        KeyAscii = 0
    End If
End Sub

hope it is useful.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
My code is nothing more than an example of how to add text to a richtextbox without using concatenation.  The two textboxes simply give an easy way to input text from two simulated "users".  Previous text formatting is not changed in any way.  My hope was that Tai-San would learn something about how the richtextbox works and implement a better solution to the problem.

You should try running it yourself rekhasri...

I'm assuming you won't, so here is how to do it using the same sub as Tai-San:

Private Sub RichTextBox2_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        With RichTextBox1
            Bef = Len(.Text)
            .SelStart = Len(.Text)
            .SelLength = 0
            .SelColor = vbBlack
            .SelFontName = "MS Sans Serif"
            .SelBold = False
            .SelFontSize = 10
            .SelText = IName & " says:" & vbCrLf
       
            .SelColor = vbRed
            .SelFontName = "Courier New"
            .SelBold = True
            .SelFontSize = 8
            .SelText = RichTextBox2.Text & vbCrLf & vbCrLf
        End With
        RichTextBox2.Text = ""
        KeyAscii = 0
        kk = 0
    End If
End Sub

Regards,

Idle_Mind
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
This line:

Bef = Len(.Text)

was a leftover from the old code and is not necessary.
0
rekhasriCommented:
idle_mind,

definitely looks like a better soln.. it didnt strike me to use the .seltext property..

i think tai-san shd use this one..

~Rekhasri
0
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
Visual Basic Classic

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.