简体   繁体   English

如何在richTextBox中更改特定字符串的颜色

[英]How to change color for specific string in richTextBox

I'm trying to change color for specific string into the richTextBox with opening of text document inside it to already existing string or added through the richTextBox to keep other text saved and black except specific line. 我正在尝试将特定字符串的颜色更改为richTextBox其中的文本文档打开为已存在的字符串,或者通过richTextBox添加以保持其他文本的保存和黑色(特定行除外)。

I got inside the richTextBox_TextChanged event, but does not works proper for me. 我进入了richTextBox_TextChanged事件,但不适用于我。 It changes text for specific string well and keeps other text black, but in all cases I have same two problems, first: 它很好地更改了特定字符串的文本,并使其他文本保持黑色,但是在所有情况下,我都有两个相同的问题,首先是:

If I add text to specific colored red string from richTextBox merged with other word or character, for example: 如果我将文本添加到来自richTextBox并与其他单词或字符合并的特定红色字符串中,例如:

if text document content is: 如果文本文档的内容是:

some string
some string
red string

and if I add to it something like: 如果我添加到类似:

some string
some string
xred string 

or: 要么:

some string
some string
red stringx

result becomes second, if I add another one string equal to "red string" : 如果我添加另一个等于"red string"字符串,结果将成为第二个:

some string
some string
red stringx        // << This line remains red
red string         // << and this does not changes and remains black

And another problem, if I write text after red string in richTextBox , all following text at the stage of writing also becomes red. 另一个问题是,如果我在richTextBox红色字符串之后编写文本,那么在编写阶段所有随后的文本也会变为红色。

For example string which must be red is: 例如必须为红色的字符串为:

string Str = "red string";

this way: 这条路:

   Color aColor = Color.FromName(Str.Split(' ')[0]);
   if (richTextBox1.Text.Contains(Str) && aColor != Color.Red)
   {
       richTextBox1.Select(richTextBox1.Text.IndexOf(Str), Str.Length);
       richTextBox1.SelectionColor = Color.Red;
   }

or this way: 或者这样:

   Color aColor = Color.FromName(Str.Split(' ')[0]);
   if (richTextBox1.Text.Contains(Str) && aColor != Color.Red)
   {
       richTextBox1.Find(Str); 
       richTextBox1.SelectionColor = Color.Red;
   }

Or this way, which can contain listed strings to change colors for each different color if needed this way string[] words = { "specword1", "specword2" }; 或通过这种方式,可以包含列出的字符串,以便在需要时为每种不同的颜色更改颜色string[] words = { "specword1", "specword2" }; , but in this case just shown as different way to do it, with same and only needed value from above, and empty string[] words : ,但在这种情况下,只是显示为不同的处理方式,上面具有相同且仅需要的值,并且为空string[] words

  string[] words = { "" };
  Color[] colors = { Color.Red };
  for (int i = 0; i < words.Length; i++)
  {
        string word = words[i];
        Color color = colors[i];
     {
        richTextBox1.Find(Str);
        richTextBox1.SelectionColor = color;
     }
  }

I got same two problems in result with all attempts, and if I use it with Form1_Load it just does not makes any changes to color. 所有尝试的结果都是两个相同的问题,如果我将其与Form1_Load一起使用,则不会对颜色进行任何更改。

So I'm wondering what can be solution for this case, and only one thing comes to mind, but not sure if it is proper way to solve this problems: 因此,我想知道对于这种情况有什么解决方案,只有一件事想到了,但是不确定是否是解决此问题的正确方法:

I'm not sure how, but somehow to make unavailable editing of red string, which is always is on separate line in text document, and at the same time do not allow to write or paste it by hand into the richTextBox . 我不确定如何操作,但是无法以某种方式对红色字符串进行编辑,该字符串始终位于文本文档的单独行中,并且同时不允许手动将其写入或粘贴到richTextBox

Anyway if it can help with first problem, seems like it does not helps to avoid changing of color for following text after red string. 无论如何,如果它可以帮助解决第一个问题,似乎无助于避免在红色字符串后跟随文本更改颜色。

inside richTextBox_TextChanged check the full line of text when comparing richTextBox_TextChanged内部检查比较时的整行文本

string str = "red string";
for(int i=0; i<richTextBox1.Lines.Length; i++) 
{ 
   string text = richTextBox1.Lines[i];
   richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(i), text.Length); 
   if(text ==str)
   {
    richTextBox1.SelectionColor = Color.Red;
   }else
   {
     richTextBox1.SelectionColor = Color.Black;
   }    
 }

for multiple colors, I would use dictionary 对于多种颜色,我将使用字典

var dictionary = new Dictionary<string, System.Drawing.Color>();
    dictionary.Add("red color", System.Drawing.Color.Red);
    dictionary.Add("Blue color", System.Drawing.Color.Blue);
//as above example you can use for loop and get each line of rich textbox
string linefromTextBox = "Blue color";
//then check that line contain of of text in the dictionaly 
if (dictionary.ContainsKey(linefromTextBox))
{
   // if key found then you can get the color as below
   // asign this as SelectionColor 
   //before that you need to Select the line from rich text box as above example
   var color = dictionary[linefromTextBox];
} 

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

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