有没有办法设置不区分大小写,以突出显示在richtextbox中有大写和小写的单词?

时间:2012-08-21 01:34:07

标签: c# winforms

真正的作者名称是:PolarBear。

我的搜索功能PolarBear,结果PolarBear按照我的意愿突出显示。

我搜索了PolarbearpolarbearpoLarbearPolarBear确实出现在结果中,但未按照我的意愿突出显示。

如何使突出显示不区分大小写与搜索一样?谢谢。

突出显示代码:

    private void searchComByAuthor()
    {
        // Process the list of files found in the directory. 
        string[] fileEntries = Directory.GetFiles(sourceDirXML);
        foreach (string fileName in fileEntries)
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

            string docPath = fileName;

            xmlDoc.Load(docPath); //* load the XML document from the specified file.

            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

            foreach (XmlNode node in nodeList)
            {

                XmlElement itemElement = (XmlElement)node;

                string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;

                if (itemAuthor.ToLower() == txtComAuthor.Text.ToString().ToLower())
                {
                    string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                    string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
                    string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
                    string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);

                    richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate +
                                                "\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
                }
            }
        }

        int pointer = 0;
        int index = 0;
        string keyword = txtComAuthor.Text;
        string shadow = richComByTemplate.Text;

        while (true)
        {
            //Searching in the copy/shadow
            index = shadow.IndexOf(keyword, pointer);
            //if keyword not found then the loop will break
            if ((index == -1) || (String.IsNullOrEmpty(keyword)))
            {
                break;
            }
            richComByTemplate.Select(index, keyword.Length);
            richComByTemplate.SelectionColor = Color.Red;
            richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
            pointer = index + keyword.Length;
        }

2 个答案:

答案 0 :(得分:2)

在设置.ToLower()keyword变量时添加shadow。然后突出显示应该按预期工作:

string keyword = txtComAuthor.Text.ToLower();
string shadow = richComByTemplate.Text.ToLower();

或者,您可以告诉IndexOf不区分大小写:

index = shadow.IndexOf(keyword, pointer, StringComparison.OrdinalIgnoreCase); 

答案 1 :(得分:1)

您可以尝试使用使用IndexOf枚举的StringComparison方法的重载。

index = shadow.IndexOf(keyword, pointer,StringComparison.InvariantCultureIgnoreCase); 

来自第二个MSDN链接:

  

当您调用字符串比较方法(如String.Compare,String.Equals或String.IndexOf)时,应始终调用包含StringComparison类型参数的重载,以便您可以指定方法的比较类型施行。有关更多信息,请参阅.NET Framework中使用字符串的最佳实践。

小型工作测试示例:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int pointer = 0;
        int index = 0;
        string keyword = txtComAuthor.Text;
        string shadow = richComByTemplate.Text;

        while (true)
        {
            //Searching in the copy/shadow 
            index = shadow.IndexOf(keyword, pointer, StringComparison.InvariantCultureIgnoreCase);
            //if keyword not found then the loop will break 
            if ((index == -1) || (String.IsNullOrEmpty(keyword)))
            {
                break;
            }
            richComByTemplate.Select(index, keyword.Length);
            richComByTemplate.SelectionColor = Color.Red;
            richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
            pointer = index + keyword.Length;
        } 

    }
}