简体   繁体   English

为什么当尝试为richTextBox中的文本的多于一部分的颜色着色时却根本没有着色?

[英]Why when trying to color more then one part of text in richTextBox it's not coloring at all?

It will color one part of text. 它将为文本上色。

In listView selectedindex event 在listView中selectedIndex事件

void lvnf_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ListViewCostumControl.lvnf.SelectedItems.Count > 0)
    {
        string word = textBox1.Text;
        string[] test = word.Split(',');
        foreach (string myword in test)
        {
            richTextBox1.Text = File.ReadAllText(ListViewCostumControl.lvnf.Items[ListViewCostumControl.lvnf.SelectedIndices[0]].Text);
            FileInfo fi = new FileInfo(ListViewCostumControl.lvnf.Items[ListViewCostumControl.lvnf.SelectedIndices[0]].Text);
            HighlightPhrase(richTextBox1, myword, Color.Yellow);
            if (myword == "Form1")
            {
                break;
            }
        }
    }
}

And

static void HighlightPhrase(RichTextBox box, string phrase, Color color)
{
    int pos = box.SelectionStart;
    string s = box.Text;
    for (int ix = 0; ; )
    {
        int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
        if (jx < 0) { break; }
        box.SelectionStart = jx;
        box.SelectionLength = phrase.Length;
        box.SelectionColor = color;
        ix = jx + 1;
    }
    box.SelectionStart = pos;
    box.SelectionLength = 0;
}

When i added this break part: 当我添加此中断部分:

if (myword == "Form1")
{
    break;
}

It's working it will color all the places in richTextBox with Form1. 它正在使用Form1为richTextBox中的所有位置着色。 But if i remove this break part it should color all places in the richTextBox that the texts is exist from the string array variable test. 但是,如果我删除此中断部分,则应为richTextBox中所有的颜色上色,该文本存在于字符串数组变量test中。

In test i have: Form1,form2,hi,44 在测试中,我有:Form1,form2,hi,44

But it's not coloring at all nothing. 但这根本没有着色。

When you removed the if (myword == "Form1") break; 当您删除if (myword == "Form1") break; part, the code pretty much becomes: 部分,代码几乎变成:

foreach (string myword in test)
            {
                richTextBox1.Text = ......;
                HighlightPhrase(richTextBox1, myword, Color.Yellow);                       
             }

which in your case since your test string is Form1,form2,hi,44, it becomes: 在您的情况下,由于您的测试字符串为Form1,form2,hi,44,因此它将变为:

 richTextBox1.Text = ......;
 HighlightPhrase(richTextBox1, "Form1", Color.Yellow);
 richTextBox1.Text = ......;
 HighlightPhrase(richTextBox1, "form2", Color.Yellow);
 richTextBox1.Text = ......;
 HighlightPhrase(richTextBox1, "hi", Color.Yellow);
 richTextBox1.Text = ......;
 HighlightPhrase(richTextBox1, "44", Color.Yellow);

As you can see the text property keeps changing and thus the formatting gets reset (or applied to your entire string in some cases). 如您所见,text属性不断变化,因此格式被重置(或在某些情况下应用于您的整个字符串)。

Making a few assumptions, I would change your code to: 做一些假设,我将您的代码更改为:

if (ListViewCostumControl.lvnf.SelectedItems.Count > 0)
        {
            var selectedText = ListViewCostumControl.lvnf.Items[ListViewCostumControl.lvnf.SelectedIndices[0]].Text;
            richTextBox1.Text = File.ReadAllText(selectedText);
            FileInfo fi = new FileInfo(selectedText);  //not used!

            string word = textBox1.Text;
            string[] test = word.Split(',');
            foreach (string myword in test)
            {                    
                HighlightPhrase(richTextBox1, myword, Color.Yellow);
            }
          }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM