Solved

rich text box

Posted on 2003-11-25
12
418 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 85

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 85

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 85

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 85

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now