Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to play around with rich text boxes at the moment, and appending information to it line by line, which seems to be working. But,

When a certain string comes through "xred", "xblk" I want the text colour to come through in a different colour.

the xred will come on a certain line and then from then on, it needs to append the text to the textbox in that colour, until the xblk string is read, when xblk is read, it needs to append everything in black until it hits the xred string.

At the moment, I have the following

VB
Dim linecount As Integer = 0
        Dim strreader As New StringReader(datastring)
        Dim line As String

        Do
            linecount = linecount + 1
            line = strreader.ReadLine()
            If line.Contains("xred") Then
                currenttb.ForeColor = Color.Red
            End If
            If line.Contains("xblk") Then
                currenttb.ForeColor = Color.White
            End If
            currenttb.AppendText(line)
        Loop Until linecount = 15



I do understand why it's not working. As I am telling the textbox to change colour on all text, rather than the line just read.

How can I set it so it appends lines in a different colour? At the moment, it seems to change the entire textbox forecolour to the last setting of the if statements.
Posted

1 solution

You can't having multiple colors in TextBox, use RichTextBox

http://stackoverflow.com/questions/2527700/change-color-of-text-within-a-winforms-richtextbox[^]
http://stackoverflow.com/questions/10587715/multi-color-textbox-c-sharp[^]

VB
Dim length As Integer = currenttb.TextLength
currenttb.SelectionStart = length
currenttb.SelectionLength = line.Length
If line.Contains("xred") Then
    currenttb.SelectionColor = Color.Red
End If
If line.Contains("xblk") Then
    currenttb.SelectionColor = Color.White
End If
currenttb.AppendText(line)
 
Share this answer
 
v3
Comments
Mendaharin 13-May-14 0:24am    
Thanks Damith - I was using richtextboxes - it was the selectioncolor part I didn't have. I just had tb.forecolor..
DamithSL 13-May-14 0:31am    
check my updated answer
DamithSL 13-May-14 0:41am    
do you still having issue?
Mendaharin 13-May-14 1:46am    
Yeah all good cheers mate :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900