我如何彩色文本的一个RichTextBox的特定部分? [英] How do i color a specific part of text in a richTextBox?

查看:159
本文介绍了我如何彩色文本的一个RichTextBox的特定部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人无效LoadKeys(词典<字符串列表<串>>字典,字符串文件名)
{
串线= System.String.Empty;
使用(StreamReader的SR =新的StreamReader(关键字))
{
,而((行= sr.ReadLine())!= NULL)
{
串[ ]令牌= line.Split(,);
dictionary.Add(令牌[0],tokens.Skip(1).ToList());
richTextBox2.AppendText(URL:+令牌[0] + - +localY坐标关键词:+令牌[1] + Environment.NewLine);
ColorText(richTextBox2,Color.Red);
}
}
}

和功能ColorText:

 公共无效ColorText(RichTextBox的彩盒,彩颜色)
{
box.SelectionStart = box.TextLength; box.SelectionLength = 0;
box.SelectionColor =颜色;
box.SelectionColor = box.ForeColor;
}



但在红色它没有颜色什么。没有改变。
我希望能够以红色颜色〔实​​施例仅令牌[0]和绿色令牌[1]例如。



我该怎么办呢?


解决方案

 公共无效ColorText(RichTextBox的彩盒,彩颜色)
{
box.Select(开始,5);
box.SelectionColor =颜色;
}


private void LoadKeys(Dictionary<string,List<string>> dictionary, string FileName)
        {
           string line = System.String.Empty;
           using (StreamReader sr = new StreamReader(keywords))
           {
            while ((line = sr.ReadLine()) != null)
            {
                string[] tokens = line.Split(',');
                dictionary.Add(tokens[0], tokens.Skip(1).ToList());
                richTextBox2.AppendText("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]+Environment.NewLine);
                ColorText(richTextBox2, Color.Red);
            }
           } 
        }

And the function ColorText:

public void ColorText(RichTextBox box, Color color)
        {
            box.SelectionStart = box.TextLength; box.SelectionLength = 0;
            box.SelectionColor = color;
            box.SelectionColor = box.ForeColor;
        } 

But it didnt color anything in Red. Nothing changed. I want to be able to color in Red for exmaple only the tokens[0] and in Green tokens[1] for example.

How can i do it ?

解决方案

public void ColorText(RichTextBox box, Color color)
        {
            box.Select(start, 5);
            box.SelectionColor = color;
        } 

这篇关于我如何彩色文本的一个RichTextBox的特定部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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