Momoro
Momoro

Reputation: 617

How would I create a "Find Form" Like in Notepad/Notepad++? C#

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?

Upvotes: 0

Views: 281

Answers (2)

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

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

Upvotes: 1

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

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

Upvotes: 1

Related Questions