C#实现记事本查找与替换功能
程序员文章站
2023-11-14 23:32:04
看了网上关于记事本的查找替换很多,但都没有达到我想要的结果,然后自己学习总结了以下的方法:
先上效果图
定义全局变量
#region =★*★*★=...
看了网上关于记事本的查找替换很多,但都没有达到我想要的结果,然后自己学习总结了以下的方法:
先上效果图
定义全局变量
#region =★*★*★= 〖查找替换〗的自定义变量 =★*★*★= /// <summary> 获取或设置查找起始位置 </summary> static int index = 0; /// <summary> 查找到的字符串数量 </summary> static int count = 0; /// <summary> 查找字符是否变更 static string stre = string.empty; /// <summary> 记录原始光标位置 </summary> static int memoindex = -1; /// <summary> 过度字符串变量 </summary> static string str = string.empty; /// <summary> 实例化富文本 </summary> static richtextbox r = new richtextbox(); #endregion
向上查找
#region =★*★*★= findup() 查找指定〖字符 或 字符串〗 =★*★*★= /// <summary>向上查找指定〖字符 或 字符串〗(不区分大小写)<para> <para> /// 参数1(rbox):内容文本框,指定为 richtextbox 文本框类型<para> /// 参数2(str):用户指定要查找的字符串</para> /// </para></para> </summary> /// <param name="rbox">内容文本框,指定为 richtextbox 文本框类型</param> /// <param name="str">用户指定要查找的字符串</param> public static void findup(richtextbox rtxt, string str) { if (stre != str) { rtxt.selectionstart = rtxt.text.length; stre = str; } index = rtxt.selectionstart; index = rtxt.find(str, 0, index, richtextboxfinds.reverse); if (index >= 0) { rtxt.selectionstart = index; rtxt.selectionlength = str.length; count++; rtxt.focus(); mycolor(rtxt);//设定关键字为颜色 if (index <= 0) { seeks(str); count = 0; } } else if (index < 0) { seeks(str); count = 0; } } #endregion
向下查找
#region =★*★*★= finddown() 查找指定〖字符 或 字符串〗=★*★*★= /// <summary>向下 查找指定〖字符 或 字符串〗<para> </para> /// 参数1(rbox):内容文本框,指定为 richtextbox 文本框类型 <para></para> /// 参数2(findstr):用户指定要查找的字符串<para></para> /// 参数3(truefales):bool 真:限定大小写 假:不分大小写</summary> /// <param name="rbox">内容文本框,指定为 richtextbox 文本框类型</param> /// <param name="findstr">用户指定要查找的字符串</param> /// <param name="truefales">bool 真:限定大小写 假:不分大小写</param> public static void finddown(richtextbox rtxt, string findstr,bool truefales) { if (truefales) filefinddownmax(rtxt, findstr); else filefinddownmin(rtxt, findstr); } #endregion #region =★*★*★= 简单的从上到下查找 =★*★*★= /// <summary>向下查找指定〖字符 或 字符串〗(限定大小写)<para> <para> /// 参数1(rbox):内容文本框,指定为 richtextbox 文本框类型 /// <para>参数2(findstr):用户指定要查找的字符串</para> /// </para></para> </summary> /// <param name="rbox">内容文本框,指定为 richtextbox 文本框类型</param> /// <param name="findstr">用户指定要查找的字符串</param> private static void finddownmax(richtextbox rtxt, string findstr) { index = rtxt.find(findstr, index, richtextboxfinds.matchcase); if (index < 0 || index > rtxt.text.length) { seeks(findstr); count = 0; index = 0; } else { rtxt.select(index, findstr.length); mycolor(rtxt);//设定关键字为颜色 index += findstr.length; rtxt.focus(); } } /// <summary>向下查找指定〖字符 或 字符串〗(不区分大小写)<para> <para> /// 参数1(rbox):内容文本框,指定为 richtextbox 文本框类型 /// <para>参数2(findstr):用户指定要查找的字符串</para> /// </para></para> </summary> /// <param name="rbox">内容文本框,指定为 richtextbox 文本框类型</param> /// <param name="findstr">用户指定要查找的字符串</param> private static void finddownmin(richtextbox rtxt, string findstr) { index = rtxt.find(findstr, index, richtextboxfinds.none); if (index < 0 || index > rtxt.text.length) { seeks(findstr); count = 0; index = 0; } else { rtxt.select(index, findstr.length); mycolor(rtxt);//设定关键字为颜色 index += findstr.length; rtxt.focus(); } } #endregion //* 类似 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) { seeks(findstring); count = 0; index = 0; memoindex = -1; } else { rtxt.select(index, findstring.length); mycolor(rtxt);//设定关键字为颜色 rtxt.focus(); count++; index += findstring.length; if (index > str.length - findstring.length) { seeks(findstring); memoindex = -1; index = 0; count = 0; } } } 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) { seeks(findstring); count = 0; index = 0; memoindex = -1; } else { rtxt.select(index, findstring.length); mycolor(rtxt);//设定关键字为颜色 rtxt.focus(); index += findstring.length; count++; if (index > str.length - findstring.length) { seeks(findstring); memoindex = -1; index = 0; count = 0; } } } else { seeks(findstring); memoindex = -1; index = 0; count = 0; } } else { rtxt.select(index, findstring.length);//在richtextbox控件中搜索关键字 index += findstring.length;//递增标识查询关键字的初始长度 mycolor(rtxt);//设定关键字为颜色 rtxt.focus(); count++; } } else { index = 0; index = r.find(findstring, index, richtextboxfinds.none); if (index < r.text.length - findstring.length && index >= 0) { r.select(index, findstring.length); rtxt.select(index, findstring.length); mycolor(rtxt);//设定关键字为颜色 index += findstring.length;//递增标识查询关键字的初始长度 rtxt.focus(); count++; } else { seeks(findstring); memoindex = -1; index = 0; count = 0; } } } /// <summary> 类似 vs 的查找方法(不区分大小写向下查找)<para> </para> /// 参数1(txt):richtextbox文本框<para></para> /// 参数2(findstring):查找字符串</summary> /// <param name="txt">richtextbox文本框</param> /// <param name="findstring">查找字符串</param> private static void filefinddownmin(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); 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.matchcase); if (index < 0 || index > r.text.length) { seeks(findstring); count = 0; index = 0; memoindex = -1; } else { rtxt.select(index, findstring.length); mycolor(rtxt);//设定关键字为颜色 rtxt.focus(); count++; index += findstring.length; if (index > str.length - findstring.length) { seeks(findstring); memoindex = -1; index = 0; count = 0; } } } else if (index < rtxt.text.length && index >= 0) { index = rtxt.find(findstring, index, richtextboxfinds.matchcase); if (index == -1) { if (r.text != "") { index = 0; index = r.find(findstring, index, richtextboxfinds.matchcase); if (index < 0 || index > r.text.length) { seeks(findstring); count = 0; index = 0; memoindex = -1; } else { rtxt.select(index, findstring.length); mycolor(rtxt);//设定关键字为颜色 rtxt.focus(); index += findstring.length; count++; if (index > str.length - findstring.length) { seeks(findstring); memoindex = -1; index = 0; count = 0; } } } else { seeks(findstring); memoindex = -1; index = 0; count = 0; } } else { rtxt.select(index, findstring.length);//在richtextbox控件中搜索关键字 index += findstring.length;//递增标识查询关键字的初始长度 mycolor(rtxt);//设定关键字为颜色 rtxt.focus(); count++; } } else { index = 0; index = r.find(findstring, index, richtextboxfinds.matchcase); if (index < r.text.length - findstring.length && index >= 0) { r.select(index, findstring.length); rtxt.select(index, findstring.length); mycolor(rtxt);//设定关键字为颜色 index += findstring.length;//递增标识查询关键字的初始长度 rtxt.focus(); count++; } else { seeks(findstring); memoindex = -1; index = 0; count = 0; } } } #endregion
替换 与 全部替换(功能不是很全)
#region =★*★*★= 〖替换〗与〖全部替换〗 =★*★*★= #region =★*★*★= 单次替换字符串 =★*★*★= /// <summary> 替换 </summary> /// <param name="rtxt">richtextbox</param> /// <param name="findstring">查找字符串</param> /// <param name="replstring">替换字符串</param> /// <param name="truefalse">(bool值)区分大小写<para> </para>真:区分<para></para>假:不区分</param> public static void replaces(richtextbox rtxt, string findstring, string replstring, bool truefalse) { string txtst = rtxt.selectedtext; if (findstring.equals(txtst)) { int fs = findstring.length; int rs = replstring.length; int cs = fs - rs; rtxt.selectionlength = findstring.length; rtxt.selectioncolor = color.coral; rtxt.selectedtext = replstring;//textbox2中放要替换的字符 rtxt.focus(); memoindex = -1; rtxt.selectionstart += cs; } finddown(rtxt, findstring, truefalse); } #region =★*★*★= 替换字符串 =★*★*★= /// <summary>替换字符串<para> </para> /// 参数1(findstring):查找的内容<para></para> /// 参数2(replstring):要替换的内容 </summary> /// <param name="findstring">查找的内容</param> /// <param name="replstring">要替换的内容</param> private static void replace(richtextbox rtxt, string findstring, string replstring) { rtxt.selectionlength = findstring.length; rtxt.selectedtext = replstring;//textbox2中放要替换的字符 index = rtxt.selectionstart;//获取或设置光标选定位置 } #endregion #endregion #region =★*★*★= 〖全部替换〗 =★*★*★= /// <summary> 全部替换<para> </para> /// 参数1(txt):richtextbox 文本框<para></para> /// 参数2(findstring):查找字符串<para></para> /// 参数3(replstring):替换字符串<para></para> /// 参数4(truefales):bool 真:区分大小写,假:不区分大小写</summary> /// <param name="txt">richtextbox 文本框</param> /// <param name="findstring">查找字符串</param> /// <param name="replstring">替换字符串</param> /// <param name="truefales">bool 真:区分大小写,假:不区分大小写</param> public static void replall(richtextbox rtxt, string findstring, string replstring, bool truefales) { if (truefales) ondistincase(rtxt, findstring, replstring); else offdistincase(rtxt, findstring, replstring); } #region =★*★*★= 全部替换(区分/不区分大小写) =★*★*★= /// <summary> 全部替换(区分大小写) </summary> /// <param name="rtxt">richtextbox 文本框</param> /// <param name="findstr">查找字符串</param> /// <param name="replstr">替换字符串</param> private static void ondistincase(richtextbox rtxt, string findstr, string replstr) { while (index >= 0 || index <= rtxt.text.length) { int cot = findstr.length - replstr.length; index = rtxt.find(findstr, index, richtextboxfinds.matchcase); if (index < 0 || index > rtxt.text.length) { dialogresult r = messagebox.show(string.format("查找结束,已经为您找到{0}个结果。", count), "查找结果-mxdr温馨提示!!", messageboxbuttons.ok, messageboxicon.warning); if (r == dialogresult.yes) count = 0; index = 0; break; } else { rtxt.select(index, findstr.length); replace(rtxt, findstr, replstr); index += findstr.length; count++; } } } /// <summary> 全部替换(不区分大小写) </summary> /// <param name="rtxt">richtextbox 文本框</param> /// <param name="findstr">查找字符串</param> /// <param name="replstr">替换字符串</param> private static void offdistincase(richtextbox rtxt, string findstr, string replstr) { while (index >= 0 || index <= rtxt.text.length) { int cot = findstr.length - replstr.length; index = rtxt.find(findstr, index, richtextboxfinds.none); if (index < 0 || index > rtxt.text.length) { dialogresult r = messagebox.show(string.format("查找结束,已经为您找到{0}个结果。", count), "查找结果-mxdr温馨提示!!", messageboxbuttons.ok, messageboxicon.warning); if (r == dialogresult.yes) count = 0; index = 0; break; } else { rtxt.select(index, findstr.length); replace(rtxt, findstr, replstr); index += findstr.length; count++; } } } #endregion #endregion #endregion
消息弹窗
#region =★*★*★= 消息提示,提示用户查找结果 =★*★*★= /// <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), "查找 - mxdr温馨提示!"); } #endregion
查找字符着色
#region =★*★*★= 查找字符上色 =★*★*★= /// <summary> 查找字符上色 </summary> /// <param name="rtxt">richtextbox</param> private static void mycolor(richtextbox rtxt) { rtxt.selectioncolor = color.coral; } #endregion
以上都为个人的理解,有不全的地方请各位能指点迷津。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。