类似VS查找功能

类似VS查找功能

这是我在一年前发的帖子 点击链接

        #region  =★*★*★= 类似 VS 的查找方法 =★*★*★=
        /// <summary>  类似 VS 的查找方法(不区分大小写向下查找)<para> </para>
        /// 参数1(txt):RichTextBox文本框<para></para>
        /// 参数2(FindString):查找字符串</summary>
        /// <param name="txt">RichTextBox文本框</param>
        /// <param name="FindString">查找字符串</param>
        private static void fileFindDownMax(RichTextBox rtxt, string FindString)
        {
            if (MemoIndex < 0)//锚定初始位置
            {
                MemoIndex = rtxt.SelectionStart;
                if (MemoIndex > 10 && MemoIndex + FindString.Length <= rtxt.Text.Length)
                {
                    str = rtxt.Text.Substring(0, MemoIndex + FindString.Length - 1);//截取光标位置 + 查找字符数 - 1 位数
                    Index = rtxt.SelectionStart;
                    r.Text = str;
                }
                else
                {
                    str = rtxt.Text;
                    r.Text = rtxt.Text;
                    Index = rtxt.SelectionStart;
                }
            }

            if (Index < str.Length - (FindString.Length - 1) && Index >= 0)
            {
                Index = r.Find(FindString, Index, RichTextBoxFinds.None);
                if (Index < 0 || Index > r.Text.Length)
                {
                    seek(FindString);
                }
                else
                {
                    FindIndex(rtxt, FindString);


                    if (Index > str.Length - FindString.Length)
                    {
                        seek(FindString);
                    }
                }
            }
            else if (Index < rtxt.Text.Length && Index >= 0)
            {
                Index = rtxt.Find(FindString, Index, RichTextBoxFinds.None);
                if (Index == -1)//(Index >= txt.Text.Length && Index <= 0)
                {
                    if (r.Text != "")
                    {
                        Index = 0; Index = r.Find(FindString, Index, RichTextBoxFinds.None);
                        if (Index < 0 || Index > r.Text.Length)
                        {
                            seek(FindString);
                        }
                        else
                        {
                            FindIndex(rtxt, FindString);

                            if (Index > str.Length - FindString.Length)
                            {
                                seek(FindString);
                            }
                        }
                    }
                    else
                    {
                        seek(FindString);
                    }
                }
                else
                {
                    FindIndex(rtxt, FindString);
                }
            }
            else
            {
                Index = 0; Index = r.Find(FindString, Index, RichTextBoxFinds.None);
                if (Index < r.Text.Length - FindString.Length && Index >= 0)
                {

                    r.Select(Index, FindString.Length);//?是否有存在必要

                    FindIndex(rtxt, FindString);
                }
                else
                {
                    seek(FindString);
                }
            }
        }
        /// <summary> 查找到字符串后,把光标位置向后推移N位(N为查找字符串长度)</summary>
        /// <param name="rtxt">RichTextBox</param>
        /// <param name="FindString">查找字符串</param>
        static void FindIndex(RichTextBox rtxt, String FindString)
        {
            rtxt.Select(Index, FindString.Length);
            myColor(rtxt);//设定关键字为颜色
            Index += FindString.Length;//递增标识查询关键字的初始长度
            rtxt.Focus();
            count++;
        }
        /// <summary>查找字符着色 以及 数值归零 </summary>
        /// <param name="FindString">着色字符串[查找的字符]</param>
        static void seek(String FindString)
        {
            seeks(FindString);
            MemoIndex = -1;
            Index = 0;
            count = 0;
        }
        /// <summary> 查找字符上色 </summary>
        /// <param name="rtxt">RichTextBox</param>
        private static void myColor(RichTextBox rtxt)
        {
            rtxt.SelectionColor = Color.Coral;
        }
        /// <summary> 消息提示,提示用户查找结果<para> <para>  
        /// 参数1(str):用户指定要查找的字符串</para></para> </summary>  
        /// <param name="str">用户指定要查找的字符串</param>  
        public static void seeks(string str)
        {
            if (count != 0)
                MessageBox.Show(string.Format("已查找完毕,共〖{0}〗个 \"{1}\"!", count, str), "查找结果-Mxdr温馨提示!!");
            else MessageBox.Show(string.Format("没有查找到 \"{0}\"!", str), "查找 - 温馨提示");
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值