Richtext formatting...

lidds

Junior Contributor
Joined
Nov 9, 2004
Messages
210
I have a richtextbox and what I want to be able to do is to format each line, so one line would be say italic and another line would be bold.

I have done the following code which changes the second line to bold, however if you click the button again to write some more text to the richtextbox the formatting of the previous text is lost:

Code:
    Private Sub btnClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClient.Click
        Dim line1 As String = "Client Says [" & Now().ToString & "]"
        Dim line2 As String = "[" & Now().ToString & "]" & " Simon - Hello how are you"

        Me.RichTextBox1.Text += line1 & vbNewLine
        textColour(line1, False)
        Me.RichTextBox1.Text += line2 & vbNewLine
        textColour(line2, True)

        Me.RichTextBox1.SelectionStart = Me.RichTextBox1.Text.Length

    End Sub

    Private Sub textColour(ByVal text As String, ByVal bold As Boolean)
        Dim newFont As Font
        RichTextBox1.SelectionStart = RichTextBox1.Find(text)

        If bold = True Then
            newFont = New Font(Me.cmbFont.Font, FontStyle.Bold)
        Else
            newFont = New Font(Me.cmbFont.Font, FontStyle.Regular)
        End If

        RichTextBox1.SelectionFont = newFont
    End Sub

I obviously need some help one keeping the format, basically I want to acheive the type of formating style that msn messenger has.

Thanks in advance

Simon
 
Back
Top