Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

rich text box

Posted on 2003-11-25
12
Medium Priority
?
427 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:Tai-San
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 9817636
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
 
LVL 2

Author Comment

by:Tai-San
ID: 9818096
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 9818168
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:Tai-San
ID: 9819333
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
 
LVL 2

Author Comment

by:Tai-San
ID: 9819499
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
 
LVL 2

Author Comment

by:Tai-San
ID: 9819824
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 9822428
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9828995
Did you try my code Tai-San?
0
 
LVL 2

Expert Comment

by:rekhasri
ID: 9829763
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9829829
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9829834
This line:

Bef = Len(.Text)

was a leftover from the old code and is not necessary.
0
 
LVL 2

Expert Comment

by:rekhasri
ID: 9829843
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

783 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