简体   繁体   English

如何使用RichTextBox.SelectionColor属性识别C#中的多个文本背景colors?

[英]How to use RichTextBox.SelectionColor property to identify multiple text's background colors in C#?

I have a RichTextBox .我有一个RichTextBox I'm trying to code so that if a specific color is found in the SelectionBackColor property of the RTB , the background color of the words/ texts to be removed.我正在尝试编码,以便如果在RTBSelectionBackColor属性中找到特定颜色,则要删除的单词/文本的背景颜色。 For that, I need to detect if there exists multiple colors in the RTB .为此,我需要检测RTB中是否存在多个 colors 。 However, according to the documentation ,但是,根据文档

If the current text selection has more than one color specified, this property returns Color.Empty.如果当前文本选择指定了不止一种颜色,则此属性返回 Color.Empty。


This is what I've tried so far:到目前为止,这是我尝试过的:

private void randomBtn_Click(object sender, EventArgs e)
{
    int startIndex = 0; //start from beginning of the richTextBox1
    int endIndex = this.richTextBox1.TextLength; //until the end of all text available
    this.richTextBox1.Select(startIndex, endIndex); //select from start until the end

    if(endIndex != 0)
    {
        for(int i=startIndex; i< endIndex; i++)
        {
            if (this.richTextBox1.Text[i].ToString().Contains(" ")) //skips white spaces
            {
                continue;
            }
            else
            {
                while ((this.richTextBox1.BackColor != Color.Empty))
                {
                    if (this.richTextBox1.SelectionBackColor.R == 155 && this.richTextBox1.SelectionBackColor.G == 255 && this.richTextBox1.SelectionBackColor.B == 255)
                    {
                        this.richTextBox1.HideSelection = true; //to prevent text highlighted
                        MessageBox.Show(this, "Texts with RGB(155, 255, 255) found!", "", MessageBoxButtons.OK);
                        break;
                    }
                    else
                    {
                        this.richTextBox1.HideSelection = true;
                        MessageBox.Show(this, "Error!", "", MessageBoxButtons.OK);
                        break;
                    }
                }
            }
        }
    }
    else
    {
        MessageBox.Show(this, "richTextBox1 is empty!", "Alert!", MessageBoxButtons.OK);
    }
}

To test, I added a breakpoint at the code containing the while line.为了测试,我在包含while行的代码处添加了一个断点。 Below shows the success and fail criterion,下面显示了成功和失败的标准,

The code works if:该代码在以下情况下有效:

  • There is no whitespace(at all)没有空格(完全)
  • There is only one color in the RTB RTB中只有一种颜色

The code fails if:如果出现以下情况,代码将失败:

  • There are whitespaces in between texts/ words/ characters文本/单词/字符之间有空格
  • There are multiple colors in the RTB RTB中有多个colors

Below shows the program execution examples:下面显示程序执行示例:

This is when only one color is applied in the RTB (success),这是在RTB中仅应用一种颜色时(成功), 例如1

This is when only one color and a whitespace are applied in the RTB (fail),这是在RTB中仅应用一种颜色和一个空格(失败)时, 例2

This is when multiple colors and whitespaces are applied in the RTB (fail),这是在RTB中应用多个 colors 和空格(失败)时, 例3


So, is there any ways to override the return value of the SelectionColor property to be able to detect multiple colors, or are there any other ways of doing this?那么,是否有任何方法可以覆盖SelectionColor属性的返回值,以便能够检测到多个 colors,或者是否有任何其他方法可以做到这一点? Just so you know, I've searched for this kind of problem over the inte.net, but I didn't think I've find any related issues.就像你知道的那样,我已经在 inte.net 上搜索过此类问题,但我认为我没有找到任何相关问题。

It took me a while to figure out what @TaW meant in the comment section, but thanks to him, I've managed to solve this issue.我花了一段时间才弄明白@TaW 在评论部分的意思,但多亏了他,我才设法解决了这个问题。

Actually, based on my reply in the comment section that I asked @TaW, what I thought as the same concept, was actually wrong.实际上,根据我在评论区的回复,我问@TaW,我认为是同一个概念,实际上是错误的。 In the post above, what I did was entirely wrong:在上面的帖子中,我所做的是完全错误的:

  1. I was supposed to assess each text one by one to know what their colors are.我应该一个一个地评估每个文本,以了解它们的 colors 是什么。 However, the codes below were already wrong to begin with.:但是,下面的代码一开始就已经是错误的。:

int startIndex = 0; int 开始索引 = 0;
int endIndex = this.richTextBox1.TextLength; int endIndex = this.richTextBox1.TextLength;
this.richTextBox1.Select(startIndex, endIndex); this.richTextBox1.Select(startIndex, endIndex);

  1. To analyze how RTB.SelectionColor , RTB.SelectionStart , and RTB.SelectionLength work, I decided to create another project.为了分析RTB.SelectionColorRTB.SelectionStartRTB.SelectionLength工作原理,我决定创建另一个项目。 The project is a simple program containing an RTB, and some other buttons to manage the RTB's SelectionColor .该项目是一个简单的程序,包含一个 RTB 和一些其他用于管理 RTB 的SelectionColor的按钮。 If you want to check for the project that I've done, you're always welcomed to visit this link.如果你想查看我做过的项目,随时欢迎你访问链接。

From " 2 ", I re-used all the codes to suit the original project that I was working on.从“ 2 ”开始,我重新使用了所有代码以适应我正在从事的原始项目。 Now, it's all fine and working as it should.现在,一切都很好,并且可以正常工作。


To note, there are two important concepts/ ideas on managing the Selection property of the RTB .需要注意的是,有两个关于管理RTBSelection属性的重要概念/想法。

  1. If you are concerned of assessing each and every text in the RTB , you should code it like this:如果您关心评估RTB中的每一个文本,您应该这样编码:
private void methodA(RichTextBox localRTB)
{
    //go through text one by one
    int startIndex = 0;
    int endIndex = localRTB.TextLength;
    localRTB.SelectionStart = startIndex;
    localRTB.SelectionLength = 1; //always 1 'cuz we want to assess each text one by one

    while (localRTB.SelectionStart < endIndex)
    {
        //if the SelectionBackColor is red, change it to white
        if (localRTB.SelectionBackColor == Color.Red) //take red color for example
        {
            localRTB.SelectionBackColor = Color.White;
        }
        //--manage bg color of selected text in RTB--

        //so that able to go to next text
        localRTB.SelectionStart += 1;
    }

    //finally...
    localRTB.DeselectAll(); //unselect text in RTB
    localRTB.Select(); //set focus back to the RTB
}

Below shows the result of the codes above( 1 ):下面显示了上述代码的结果( 1 ):
案例A

  1. If you don't really care about assessing each and every text in the RTB , you should code it like this instead:如果您真的不关心评估RTB中的每一个文本,您应该改为这样编码:
private void methodB(RichTextBox localRTB)
{
    int startIndex;
    int endIndex;

    if (localRTB.SelectionLength.Equals(0)) //if user don't select any text
    {
        startIndex = 0; //from beginning of RTB
        endIndex = localRTB.TextLength; //'til the end of RTB

        //--manage selected text in the RTB--
        localRTB.SelectionStart = startIndex;
        localRTB.SelectionLength = endIndex;

        localRTB.Select(localRTB.SelectionStart, localRTB.SelectionLength);
        //--manage selected text in the RTB--
    }
    else if (!(localRTB.SelectionLength.Equals(0))) //if user has text selected
    {
        startIndex = localRTB.SelectionStart; //from beginning of RTB
        endIndex = localRTB.SelectionLength; //'til the end of RTB

        if (localRTB.SelectedText.Contains(" ")) //skips whitespaces if selected together with text
        {
            if (localRTB.SelectedText.EndsWith(" "))
            {
                endIndex -= 1;
            }
        }

        //--manage selected text in the RTB--
        localRTB.Select(startIndex, endIndex);
        //--manage selected text in the RTB--
    }
}

Below shows the result of the codes above( 2 ):下面显示了上述代码的结果( 2 ):
案例B

Peace...✌️和平...✌️

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

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