Home > Back-end >  How to use RichTextBox.SelectionColor property to identify multiple text's background colors in
How to use RichTextBox.SelectionColor property to identify multiple text's background colors in

Time:03-25

I have a 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. For that, I need to detect if there exists multiple colors in the RTB. However, according to the eg1

This is when only one color and a whitespace are applied in the RTB(fail), eg2

This is when multiple colors and whitespaces are applied in the RTB(fail), eg3


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? Just so you know, I've searched for this kind of problem over the internet, but I didn't think I've find any related issues.

CodePudding user response:

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.

Actually, based on my reply in the comment section that I asked @TaW, what I thought as the same concept, was actually wrong. 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. However, the codes below were already wrong to begin with.:

int startIndex = 0;
int endIndex = this.richTextBox1.TextLength;
this.richTextBox1.Select(startIndex, endIndex);

  1. To analyze how RTB.SelectionColor, RTB.SelectionStart, and RTB.SelectionLength work, I decided to create another project. The project is a simple program containing an RTB, and some other buttons to manage the RTB's SelectionColor. If you want to check for the project that I've done, you're always welcomed to visit caseA

    1. If you don't really care about assessing each and every text in the RTB, you should code it like this instead:
    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):
    caseB

    Peace...✌️

  • Related