C#RichTextBox 文本查找与替换
2014-04-15 20:01
387 查看
〖欢迎转载〗转载请注明出处
把 查找,替换,全部替换 三个button 的 Enabled 设置为 false ,f替换内容TextBox Enabled 也设置为false , CheckBox2 为选中....
在主窗体中 关联一个函数就可以了,主窗体关联如下: Form_Mxdr.RichTextBox .Name=txtInput;
查找窗体代码: 向上查找无法区分大小写 ,全部替换只能替换查找内容限定区分出的大写或小写字母进行转换...
把button .Enabled 设置为false 后应该在 textBox1文本内容更改事件中把值设置回true.........
把 查找,替换,全部替换 三个button 的 Enabled 设置为 false ,f替换内容TextBox Enabled 也设置为false , CheckBox2 为选中....
在主窗体中 关联一个函数就可以了,主窗体关联如下: Form_Mxdr.RichTextBox .Name=txtInput;
/// <summary>获取 Form_Mxdr.RichTextBox<para> <para> /// 获取 RichTextBox 的读写操作权限</para></para> </summary> public RichTextBox RichTxtBox { get { return this.txtInput; } set { this.txtInput = value; } }
查找窗体代码: 向上查找无法区分大小写 ,全部替换只能替换查找内容限定区分出的大写或小写字母进行转换...
public partial class formFind : Form { public formFind() { InitializeComponent(); } private void formFind_Load(object sender, EventArgs e) { radioButton2.Checked = true; } #region ***********全局变量************* int start = 0; int sun = 0; int count = 0; #endregion #region =★*★*★= 四个 Button 点击事件 =★*★*★= private void button1_Click(object sender, EventArgs e) { Form_Mxdr f1 = (Form_Mxdr)this.Owner; RichTextBox rbox = f1.RichTxtBox; string str = this.textBox1.Text; if (this.checkBox1.Checked) //是否区分大小写 { this.FindDownM(rbox, str); } else { if (this.radioButton2.Checked) { this.FindDown(rbox, str); } else { this.FindUp(rbox, str); } } } private void button2_Click(object sender, EventArgs e) { string str0=this.textBox1.Text, str1 = this.textBox2.Text; this.replace(str0, str1); } private void button3_Click(object sender, EventArgs e) { Form_Mxdr f1 = (Form_Mxdr)this.Owner; RichTextBox rbox = f1.RichTxtBox; string str0 = textBox1.Text, str1 = textBox2.Text; this.ReplaceAll(rbox, str0, str1); } private void button4_Click(object sender, EventArgs e) { this.Close(); } #endregion #region =★*★*★= 〖查找替换〗 函数 =★*★*★= /// <summary>向上查找指定字符 或 字符串 (不区分大小写)<para> <para> /// 参数1(rbox):内容文本框,指定为 RichTextBox 文本框类型<para> /// 参数2(str):用户指定要查找的字符串</para> /// </para></para> </summary> /// <param name="rbox">内容文本框,指定为 RichTextBox 文本框类型</param> /// <param name="str">用户指定要查找的字符串</param> private void FindUp(RichTextBox rbox, string str) { int rboxL = rbox.SelectionStart; int index = rbox.Find(str, 0, rboxL, RichTextBoxFinds.Reverse); if (index > -1) { rbox.SelectionStart = index; rbox.SelectionLength = str.Length; sun++; rbox.Focus(); } else if (index < 0) { seeks(str); sun = 0; //如果还想再找一遍,添加下面这句 //rbox.SelectionStart = rbox.Text.Length; } } /// <summary>向下查找指定字符 或 字符串 (不区分大小写)<para> <para> /// 参数1(rbox):内容文本框,指定为 RichTextBox 文本框类型 /// <para>参数2(str):用户指定要查找的字符串</para> /// </para></para> </summary> /// <param name="rbox">内容文本框,指定为 RichTextBox 文本框类型</param> /// <param name="str">用户指定要查找的字符串</param> private void FindDown(RichTextBox rbox, string str) { int rboxL = rbox.Text.Length; if (start < rboxL) { start = rbox.Find(str, start, RichTextBoxFinds.None); int los = rbox.SelectionStart + str.Length; if ((start < 0) || (start > rboxL)) { if (count == 0) { this.seeks(str); start = los; sun = 0; } else { this.seeks(str); start = los; sun = 0; } } else if (start == rboxL || start < 0) { this.seeks(str); start = los; sun = 0; } else { sun++; start = los; rbox.Focus(); } } else if (start == rboxL || start < 0) { int los = rbox.SelectionStart + str.Length; this.seeks(str); start = los; sun = 0; } else { int los = rbox.SelectionStart + str.Length; this.seeks(str); start = los; sun = 0; } } /// <summary>向下查找指定字符 或 字符串 (限定大小写)<para> <para> /// 参数1(rbox):内容文本框,指定为 RichTextBox 文本框类型 /// <para>参数2(str):用户指定要查找的字符串</para> /// </para></para> </summary> /// <param name="rbox">内容文本框,指定为 RichTextBox 文本框类型</param> /// <param name="str">用户指定要查找的字符串</param> private void FindDownM(RichTextBox rbox, string str) { int rboxL = rbox.Text.Length; if (start < rboxL) { start = rbox.Find(str, start, RichTextBoxFinds.MatchCase); int los = rbox.SelectionStart + str.Length; if ((start < 0) || (start > rboxL)) { if (count == 0) { this.seeks(str); start = los; sun = 0; } else { this.seeks(str); start = los; sun = 0; } } else if (start == rboxL || start < 0) { this.seeks(str); start = los; sun = 0; } else { sun++; start = los; rbox.Focus(); } } else if (start == rboxL || start < 0) { int los = rbox.SelectionStart + str.Length; this.seeks(str); start = los; sun = 0; } else { int los = rbox.SelectionStart + str.Length; this.seeks(str); start = los; sun = 0; } } /// <summary> 消息提示,提示用户查找结果<para> <para> /// 参数1(str):用户指定要查找的字符串</para></para> </summary> /// <param name="str">用户指定要查找的字符串</param> private void seeks(string str) { if (sun != 0) MessageBox.Show(string.Format("已查找完毕,共〖{0}〗个 \"{1}\"!", sun, str), "查找 - 温馨提示"); else MessageBox.Show(string.Format("没有查找到 \"{0}\"!", str), "查找 - 温馨提示"); } /// <summary> 全部替换指定〖字符 或 字符串〗<para> <para> /// 参数1(rbox):要替换内容的文本框,指定为 RichTextBox 文本框类型<para> /// 参数2(str0):指定〖原有〗的内容(查找内容)</para><para> /// 参数3(str1):指定〖新〗的内容(替换内容)</para></para></para> </summary> /// <param name="rbox">要替换内容的文本框,指定为 RichTextBox 文本框类型</param> /// <param name="str0">指定〖原有〗的内容(查找内容)</param> /// <param name="str1">指定〖新〗的内容(替换内容)</param> private void ReplaceAll(RichTextBox rbox, string str0, string str1) { rbox.Text = rbox.Text.Replace(str0, str1); } /// <summary>单次替换字符或字符串<para> <para> /// 参数1(str0):查找的内容<para> /// 参数2(str1):要替换的内容 /// </para> </para></para> </summary> /// <param name="str0">查找的内容</param> /// <param name="str1">要替换的内容</param> private void replace(string str0,string str1) { Form_Mxdr f1 = (Form_Mxdr)this.Owner; RichTextBox rbox = f1.RichTxtBox; rbox.SelectionLength = str0.Length; rbox.SelectedText = str1;//textBox2中放要替换的字符 } #endregion private void textBox1_TextChanged(object sender, EventArgs e) { this.button1.Enabled = true; } private void checkBox2_CheckedChanged(object sender, EventArgs e) { if (this.checkBox2.Checked) { this.textBox2.Enabled = true; this.button1.Enabled = true; this.button2.Enabled = true; this.button3.Enabled = true; } else { this.textBox2.Enabled = false; this.button2.Enabled = false; this.button3.Enabled = false; } } }
把button .Enabled 设置为false 后应该在 textBox1文本内容更改事件中把值设置回true.........
private void textBox1_TextChanged(object sender, EventArgs e) { this.button1.Enabled = true; }
相关文章推荐
- C#中的委托和事件(续)
- C#中ListView的虚拟列表的使用
- C# WinForm多线程开发(三) Control.Invoke
- C# WinForm多线程开发(三) Control.Invoke
- C# 中的委托和事件
- C# WinForm多线程开发(二) ThreadPool 与 Timer
- C# WinForm多线程开发(二) ThreadPool 与 Timer
- C# WinForm多线程开发(一) Thread类库
- C# WinForm多线程开发(一) Thread类库
- C#迁移之callXBFLibrary - ADO.net
- C# 使用反射动态加载子窗体
- LINQ的演变及其对C#设计的影响
- 提高C#编程水平的50个要点 1.总是用属性 (Property) 来代替可访问的数据成员 2.在 readonly 和 const 之间,优先使用 readonly 3.在 as 和 强制类型转换之
- C# DataGridView添加右键菜单等技巧 --转
- C# 遍历文件系统,源代码分享
- c# Action,Func,Predicate委托
- C#标准命名规范
- C#接口与抽象类区别实例
- C#的c/s做出开灯关灯计算?
- C#声明一个100大小的数组 随机生成1-100之间不重复的数