0

I am currently making a Notepad-Like program, but I am stumped.

I need to make a "Find a Letter/Word" Form. I have it all figured out, but I can't seem to get it to return the RichTextBox SelectionBackColor back to default, e.g. Color.White;

The current code I have, here:

This is the Find Button for Form2.cs:

    public static void Find(RichTextBox rtb,String word, Color color)
    {
        if(word=="")
        {
            return;
        }
        int s_start = rtb.SelectionStart, startIndex = 0, index;
        while((index=rtb.Text.IndexOf(word,startIndex))!=-1)
        {
            rtb.Select(index, word.Length);
            rtb.SelectionColor = color;
            startIndex = index + word.Length;
        }
        rtb.SelectionStart = s_start;
        rtb.SelectionLength = 0;
        rtb.SelectionColor = Color.White;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Find(richtext, textBox1.Text, Color.Blue);
    }

Don't mind the references(rtb, etc.)

My problem is this: It works fine at first, but if you delete the ORIGINAL "Found" text, then the SelectionColor becomes the "Found" text's SelectionColor Does anyone have a fix?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Momoro
  • 599
  • 4
  • 23
  • 1
    You have a number of options. For example: 1) protect the selection (see [RichTextBox.SelectionProtected](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.richtextbox.selectionprotected)) so the text cannot be modified (until you decide it can be). 2) When the text is modified (KeyDown, KeyPress, TextChanged etc.), remove all current selections. 3) A mix of the above. Consider that a Selection and a HighLight can be considered and treated differently. A HighLight can be non-persistent, (e.g., generated by `TextRenderer.DrawText`). Cleared as soon as the control is invalidated. – Jimi Oct 20 '19 at 05:38
  • Hmm... No.2 Sounds like a good one, but still... Is there any way to just modify the code I posted, instead of going to such lengths? Heh, I guess I'm just rusty in this area... :) – Momoro Oct 20 '19 at 06:10
  • Actually, No. 2 & 1 helped me a lot. If you make it an answer, I'll mark it, :D – Momoro Oct 20 '19 at 08:47
  • It was just a suggestion, I don't know what you implemented. You could post the answer yourself. About the 3rd point, I mean something like this: [How to highlight wrapped text in a control using the graphics?](https://stackoverflow.com/a/48257170/7444103) (see the part related to a RichTextBox) or [this](https://stackoverflow.com/a/49338589/7444103) (written in VB.Net, but the *concept* is what matters). – Jimi Oct 20 '19 at 11:57
  • You might also be interested in the method shown here: [How to justify text in a label](https://stackoverflow.com/a/47470191/7444103). Beside Labels controls, it shows how to setup a custom RichTextBox, to enable text justification (from a single Paragraph to the whole Text), re-activating the native advanced typographics options (a .Net RichTextBox control can actually justify text on its own, it just needs a *push*). – Jimi Oct 20 '19 at 11:57

2 Answers2

1

First, I want to mention that Richtextbox default selectioncolor is black instead of white.

Second, you can try the following code to do what you want.

  private void button1_Click(object sender, EventArgs e)
        {
            Find(richTextBox1, textBox1.Text, Color.Blue);
        }

    public static void Find(RichTextBox rtb, String word, Color color)
    {
        rtb.SelectionStart = 0;
        rtb.SelectionLength = rtb.TextLength;
        rtb.SelectionColor = Color.Black;
        if (word == "")
        {
            return;
        }
        int s_start = rtb.SelectionStart, startIndex = 0, index;
        while ((index = rtb.Text.IndexOf(word, startIndex)) != -1)
        {
            rtb.Select(index, word.Length);
            rtb.SelectionColor = color;
            startIndex = index + word.Length;
        }

    }
    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
    {
        richTextBox1.SelectionColor = Color.Black;
    }

Result: enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
  • This is great, but I have a question -- Does this actually fix it? In my **original** code, whenever I "found" some text, I would delete it, then if I typed more text, it would be blue instead of original color. I'll test this :) – Momoro Oct 21 '19 at 03:36
  • This doesn't work :( After I end up deleting the text in the `RichTextBox`, it's just blue. Any fix? :) – Momoro Oct 21 '19 at 04:01
  • I am not sure what you want now. can you give a example to clarify your meaning? – Jack J Jun Oct 21 '19 at 05:10
  • My problem: Basically it's GOOD, but after I REMOVE the searched/highlighted text from the `RichTextBox`, the `SelectionColor` stays Blue. – Momoro Oct 21 '19 at 05:11
  • I have updated my code, you could have a look. You need to use selection changed event. – Jack J Jun Oct 21 '19 at 05:49
  • I'll try it :) I'll get back to you after – Momoro Oct 21 '19 at 05:52
  • I admit, Jack, that my mistake was making the `SelectionColor` White :D – Momoro Oct 21 '19 at 07:37
1

According to your new description, I suggest that you can add another button to return it to original color.

Code:

private void button1_Click(object sender, EventArgs e)
        {
            Find(richTextBox1, textBox1.Text, Color.Blue);
        }

        public static void Find(RichTextBox rtb, String word, Color color)
        {
            rtb.SelectionStart = 0;
            rtb.SelectionLength = rtb.TextLength;
            rtb.SelectionColor = Color.Black;
            if (word == "")
            {
                return;
            }
            int s_start = rtb.SelectionStart, startIndex = 0, index;
            while ((index = rtb.Text.IndexOf(word, startIndex)) != -1)
            {
                rtb.Select(index, word.Length);
                rtb.SelectionColor = color;
                startIndex = index + word.Length;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionStart = 0;
            richTextBox1.SelectionLength = richTextBox1.TextLength;
            richTextBox1.SelectionColor = Color.Black;
        }

Like the following: enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
  • Could I do this WITHOUT using a button? I'm trying to make a notepad, and I need it to do it programmatically instead of using another button :D Any suggestions? – Momoro Oct 21 '19 at 05:34