Textbox.Text -- how to make it black and red

cesarfrancisco

Active member
Joined
Apr 3, 2007
Messages
33
Programming Experience
Beginner
I used a MultiLine TextBox (tried also a RichTextBox) with a Vertical scrollbar as a calculator's tape.

I would like the negative numbers show in red and the rest in black. TextBox.ForeColor spec. will turn the whole text into a semaphore: black and red depending on the number's sign (+ or -).
 
Only RichTextBox control support different text formatting for various part of its text. I don't know how you imagine this done, also if the text is mixed text/numbers or only numbers and perhaps one number each line and/or prepending/appending signs. Alternatives could be "real-time" formatting where you prepend sign and set formatting accordingly on keypress, or run a method at some time where you search text for numbers and format them.
 
Try something like this:
VB.NET:
Private Sub AppendNumber(ByVal number As Double)
    Me.RichTextBox1.AppendText(Environment.NewLine)

    If number < 0.0 Then
        Me.RichTextBox1.SelectionColor = Color.Red
    Else
        Me.RichTextBox1.SelectionColor = Color.Black
    End If

    Me.RichTextBox1.AppendText(number.ToString())
End Sub
 
Back
Top