Solved

rich text box

Posted on 2003-11-25
12
425 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
[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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 50 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

615 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