Greetings All, I've been banging my head over this problem for some time, any advice would be greatly appreciated.

I have 5 strings that are pulled from the database. The strings are dynamic so their lengths will vary. I want to place those five strings in a richtextbox control, which I can do but I want to format each string differently. Such as String1 (font size=14, color=blue) and String2 (font size=12, color=red) etc.. The document doesn't need to be saved . I would rather do it this way rather than setting up a bunch of labels and such. Any ideas?

Recommended Answers

All 2 Replies

Try this

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '  String 1
        Dim Position As Int16 = 0
        Dim string1 As String = "This is string1. Font=Arial: FontSize=14: Color=Blue" & vbNewLine
        Dim myFont As New Font("Arial", 14, FontStyle.Regular, GraphicsUnit.Point)
        Dim myColor As Color = Color.Blue
        rtb.Select(Position, 0)
        rtb.SelectionFont = myFont
        rtb.SelectionColor = myColor
        rtb.SelectedText = string1
        Position += string1.Length

        '  String 2
        string1 = "This is string2. Font=microsoft sans serif: FontSize=10: Color=Red" & vbNewLine
        myFont = New Font("microsoft sans serif", 10, FontStyle.Regular, GraphicsUnit.Point)
        myColor = Color.Red
        rtb.Select(Position, 0)
        rtb.SelectionFont = myFont
        rtb.SelectionColor = myColor
        rtb.SelectedText = string1 
        Position += string1.Length

        '  String 3
        string1 = "This is string3. Font=courier new: FontSize=30: Color=Green: BOLD" & vbNewLine
        myFont = New Font("courier new", 30, FontStyle.Bold, GraphicsUnit.Point)
        myColor = Color.Green
        rtb.Select(Position, 0)
        rtb.SelectionFont = myFont
        rtb.SelectionColor = myColor
        rtb.SelectedText = string1 
        Position += string1.Length
    End Sub
End Class

In this code how to display the text on RTB is not included and moreover if we directly use .text = string1 then earlier text is reaplaced so does this work with appending of text

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.