1
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);
            }
           } 
        }

和功能 ColorText:

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

但它没有用红色着色任何东西。没有改变。例如,我希望能够仅将令牌 [0] 和绿色令牌 [1] 着色为红色。

我该怎么做 ?

4

3 回答 3

4
public void ColorText(RichTextBox box, Color color)
        {
            box.Select(start, 5);
            box.SelectionColor = color;
        } 
于 2012-10-12T07:17:17.120 回答
3

您在 ColorText 中显示的代码显示您将走到文本的末尾,将选择长度设置为 0,将颜色设置为红色,然后返回前景色,所以没有实现。

也许你需要做类似的事情

        box.Text = "This is a red color";
        box.SelectionStart = 10;
        box.SelectionLength = 3;
        box.SelectionColor = Color.Red;
        box.SelectionLength = 0;
于 2012-10-12T07:20:23.250 回答
0

box.SelectionStart = box.TextLength;- 这行代码可以解释为“开始突出显示从框文本末尾开始的文本”。即选择无文本,因为在文本的最后一个之后不能有任何文本。

box.SelectionLength = 0;- 此外,这一行可以解释为“突出显示 0 文本量”。你已经双重确保你没有选择文本哈哈。

我不确定您要如何确定要选择的文本,但我会使用以下内容:

        public void ColorSelectedText(RichTextBox textBox, Int32 startPos, Int32 length, Color color)
        {
            textBox.Select(startPos, length);
            textBox.SelectionColor = color;
        } 

传入你的文本框对象和你的颜色。

您传递的整数可以被认为是用鼠标光标突出显示文本:

startPos 是您向下单击鼠标的位置,“length”是 startPos 和您释放鼠标的位置之间的字符数。

于 2012-10-12T08:04:14.280 回答