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

最近自己写了一个仿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+ "/"");    //找不到则提示
        }

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值