JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Setting Text Color in RTF Boxes

Another call from the Testing Department—now the users want to use different text colors in your word processing program. Can you do that? Yes, you can, using the SelectionColor property.

To set colors in a rich text box, you can make a selection and set the rich text box's SelectionColor property. One way to set colors in VB .NET is with the Colors enumeration, using colors such as Colors.Red, Colors.Green, and so on.

The RichTextBoxes example on the CD-ROM is there to make this clearer. In it, I display the text "This rich text box supports font colors like red and blue and green" in a rich text box, and color the word "red" red, "blue" blue, and "green" green (although that'll be hard to see in the figure in this book, of course). Here's how that example looks in code:

Private Sub Button5_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button5.Click
    RichTextBox3.SelectionStart = RichTextBox3.Find("red")
    RichTextBox3.SelectionColor = Color.Red

    RichTextBox3.SelectionStart = RichTextBox3.Find("green")
    RichTextBox3.SelectionColor = Color.Green

    RichTextBox3.SelectionStart = RichTextBox3.Find("blue")
    RichTextBox3.SelectionColor = Color.Blue
End Sub

This program produces the display you see in Figure 5.11.

Figure 5.11: Coloring text in a rich text box.
Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor