.Net (C#) RichTextBox控件实现上下查找

    技术2024-07-02  63

    最近自己写了一个仿Windows的记事本程序,上下查找部分的功能不好实现,所以后面自己编写了一个查找函数。。。。。。

     

           //向下查找        public void down(RichTextBox text,string test,bool check)        {            string strDown = "";            int intCount = 0,i=0;            if(check==false)  //不区分大小写            {                if (text.SelectedText.ToUpper() == test.ToUpper())   //如果选中的文本等于需要替换的文本                {

                        //从光标所在位置后一位开始截取后面的所有字符                    strDown = text.Text.Substring(text.SelectionStart + 1, text.Text.Length - (text.SelectionStart+1));                  }                else                {

                        //从光标所在位置开始截取后面的所有字符                    strDown = text.Text.Substring(text.SelectionStart, text.Text.Length - text.SelectionStart);                }                            }            else if (check == true)   //区分大小写            {                if (text.SelectedText == test)  //如果选中的文本等于需要替换的文本                {

                        //从光标所在位置后一位开始截取后面的所有字符                    strDown = text.Text.Substring(text.SelectionStart + 1, text.Text.Length - (text.SelectionStart+1));                }                else                {

                        //从光标所在位置开始截取后面的所有字符                    strDown = text.Text.Substring(text.SelectionStart , text.Text.Length - text.SelectionStart);                }            }            intCount = text.Text.Length - strDown.Length;  //存放除截取的字符数以外的字符数

                 // i + test.Length(要查找的字符的长度) 的长度不得大于截取字符数的长度            for (i = 0; i+test.Length <= strDown.Length ; i++)             {                if (check == true)  //区分大小写                {                    if (strDown.Substring(i, test.Length) == test)  //截取的字符等于要查找的字符                    {                        text.SelectionStart = i+intCount;   //设置光标的起始位置                        text.SelectionLength = test.Length;   //设置文本的选定字符数                        return;                    }                }                else if (check == false)    //不区分大小写                {                    if (strDown.Substring(i, test.Length).ToUpper() == test.ToUpper())   //截取的字符等于要查找的字符                    {                        text.SelectionStart = i+intCount;   //设置光标的起始位置                        text.SelectionLength = test.Length;   //设置文本的选定字符数                        return;                    }                }            }            MessageBox.Show("找不到/"" + test + "/"");     //找不到则提示       }

     

     

            //向上查找        public void up(RichTextBox text, string test, bool check)        {            string strDown = "";            int  i = 0;            if (check== false)   //不区分大小写            {                if (text.SelectedText.ToUpper() == test.ToUpper())   //文本选定的内容等于要查找的内容                {                    strDown = text.Text.Substring(0, text.SelectionStart-1);    //从开头开始向后截取到光标所在位置的前一位                }                else                {                    strDown = text.Text.Substring(0,text.SelectionStart);   //从头开始向后截取到光标所在的位置                }

                }            else if (check== true)    //区分大小写            {                if (text.SelectedText == test)    //文本选定内容等于要查找的内容                {                    strDown = text.Text.Substring(0, text.SelectionStart -1);   //从开头开始向后截取到光标所在位置的前一位                }                else                {                    strDown = text.Text.Substring(0, text.SelectionStart);     //从头开始向后截取到光标所在的位置                }            }

                i = 截取的文本长度减去要查找的字符长度  (因为 Substring 是从左往右截取)            for (i = strDown.Length-test.Length; i >=0; i--)            {                if (check == true)   //区分大小写                {                    if (strDown.Substring(i, test.Length) == test)   //截取的内容等于要查找的内容                    {                        text.SelectionStart = i ;    //设置光标所在位置                        text.SelectionLength = test.Length;   //设置选定的字符数                        return;                    }                }                else if (check== false)  //不区分大小写                {                    if (strDown.Substring(i, test.Length).ToUpper() == test.ToUpper())  //截取的内容等于要查找的内容                    {                        text.SelectionStart = i ;  //设置光标所在位置                        text.SelectionLength = test.Length;  //设置选定的字符数                        return;                    }                }            }            MessageBox.Show("找不到/"" + test+ "/"");    //找不到则提示        }

     

     

     

     

    最新回复(0)