使用不同的颜色线更新文本框 [英] updating textbox with different colour lines

查看:87
本文介绍了使用不同的颜色线更新文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用富文本框,并逐行添加信息,这似乎正在起作用。但是,



当某个字符串通过xred,xblk时,我希望文本颜色以不同的颜色出现。



xred将出现在某一行上,然后从那时开始,它需要将文本附加到该颜色的文本框中,直到读取xblk字符串,当读取xblk时,它我需要将所有东西都贴上黑色直到它碰到xred字符串。



目前,我有以下



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

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







我明白为什么我这不行。因为我告诉文本框改变所有文本的颜色,而不是刚读取的行。



如何设置它以便以不同颜色附加线条?目前,似乎将整个文本框前期颜色更改为if语句的最后一个设置。




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.

推荐答案

您不能在TextBox中使用多种颜色,请使用RichTextBox



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

http://stackoverflow.com/questions/10587715/multi-color-textbox-c-sharp [ ^ ]



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[^]

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)


这篇关于使用不同的颜色线更新文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆