简体   繁体   中英

How do I make my richTextBox search case-insensitive?

I am making a word processor. The word processor has a facility which allows the user to search the richTextBox for text.

Currently, my "find" code consists of:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Basic_Word_Processor
{
    public partial class Find : Form
    {
        public Find()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int index = 0;
            string temp = Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text;
            Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text = "";
            Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text = temp;
            while (index < Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text.LastIndexOf(textBox1.Text))
            {
                Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Find(textBox1.Text, index, Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.TextLength, RichTextBoxFinds.None);
                Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.SelectionBackColor = Color.Yellow;
                index = Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text.IndexOf(textBox1.Text, index) +1;
            {
        }
    }
}

        private void Find_FormClosing(object sender, FormClosingEventArgs e)
        {
            Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.SelectionBackColor = Color.White;
        }
    }
}

However, if the richTextBox contains the word "TEST", and the user searched for "test", no results would be found.

I asked the same question on the MSDN forum, but I was told to convert the textBox and richTextBox ToLower, which worked, but it made the text in the richTextBox lower case. I do not want it to change the actual text in the richTextBox.

Any suggestions?

EDIT

The textBox is on the Find form and the richTextBox is on the Basic_Word_Processor form.

I believe C# is case sensitive by default, but there is an overload for LastIndexOf (and presumably IndexOf) that allows for a case-insensitive search by specifying parameter such as StringComparison.InvariantCultureIgnoreCase (or something similar). See http://msdn.microsoft.com/en-us/library/ms224422.aspx .

EDIT:

To clarify, I am proposing changing the code to something like:

string findText = textBox1.Text;
string currentText = Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text;
int index = currentText.IndexOf(findText, StringComparison.InvariantCultureIgnoreCase);
while (index >= 0)
{
    Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Find(findText, index, currentText.Length, RichTextBoxFinds.None);
    Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.SelectionBackColor = Color.Yellow;
    index = currentText.IndexOf(findText, index+1, StringComparison.InvariantCultureIgnoreCase);
}

However, now that I think about it, you should be able to do something simpler such as:

int index = Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Find(findText, RichTextBoxFinds.None);
while (index >= 0)
{
    Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.SelectionBackColor = Color.Yellow;
    index = Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Find(findText, index+1, RichTextBoxFinds.None);
}

You Can Use Regex

 private void button1_Click(object sender, EventArgs e)
        {
            Regex reg = new Regex("text",RegexOptions.IgnoreCase);
            foreach (Match find in reg.Matches(richTextBox1.Text))
            {
                richTextBox1.Select(find.Index, find.Length);
                richTextBox1.SelectionColor = Color.Red;
            }
        }

Result:

在此输入图像描述

Calling text1.Text.ToLower() will not make the text in your rich text box to be in lower case, because it just returning value.

You can keep it in a string:

string text = text1.Text.ToLower();

Then using the string text to process the searching.

Moreover, I don't quite sure about the Find method, but it should has a ignore case option. If not, make sure to convert the compared text to lowercase too.

EDIT:

I may be mistaken about which one is the richTextBox, text1 or Basic_Word_Processor.Instance.richTextBoxPrintCtrl1

If the case is the one in Basic_Word_processor, then in the MSDN link you give in the comment:

    string temp = Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text.ToLower();
    Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text = "";
    Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text = temp;

It basically said:

  • Convert the text in the RichTextBox to lowercase. Keep it in temp
  • Set the RichTextBox text ot empty
  • Replace the RichTextBox text with the temp

Of course it makes your RichTextBox lowercase.

EDIT2:

Ok seems like the user want to highlight all the found text by given keyword and it is not working. I'll try to answer it.

First, Rather than using the Find() method in the rich text box, use the SelectionStart and SelectionLength instead. I don't see the Find() method to give case insensitive comparison.

Example:

string richText = Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.Text.ToLower();
string keyText = textBox1.Text.ToLower();

In the while syntax, use the overload string LastIndexOf to compare non-case sensitive. This may not be a matter now as the rich is already in lower state.

while (index < richText.LastIndexOf(textBox1.Text, StringComparison.InvariantCultureIgnoreCase))

Then to find the text, use the following:

index = richText.IndexOf(keyText, index);

Then set the color like this:

Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.SelectionStart = index;
Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.SelectionLength = keyText.Length;
Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.SelectionBackColor = Color.Yellow;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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