类似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), "查找 - 温馨提示");
}