〖欢迎转载〗转载请注明出处
把 查找,替换,全部替换 三个button 的 Enabled 设置为 false ,f替换内容TextBox Enabled 也设置为false , CheckBox2 为选中....
在主窗体中 关联一个函数就可以了,主窗体关联如下: Form_Mxdr.RichTextBox .Name=txtInput;
/// 获取 Form_Mxdr.RichTextBox
/// 获取 RichTextBox 的读写操作权限
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 =★*★*★= 〖查找替换〗 函数 =★*★*★=
/// 向上查找指定字符 或 字符串 (不区分大小写)
/// 参数1(rbox):内容文本框,指定为 RichTextBox 文本框类型
/// 参数2(str):用户指定要查找的字符串
///
/// 内容文本框,指定为 RichTextBox 文本框类型
/// 用户指定要查找的字符串
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;
}
}
/// 向下查找指定字符 或 字符串 (不区分大小写)
/// 参数1(rbox):内容文本框,指定为 RichTextBox 文本框类型
/// 参数2(str):用户指定要查找的字符串
///
/// 内容文本框,指定为 RichTextBox 文本框类型
/// 用户指定要查找的字符串
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;
}
}
/// 向下查找指定字符 或 字符串 (限定大小写)
/// 参数1(rbox):内容文本框,指定为 RichTextBox 文本框类型
/// 参数2(str):用户指定要查找的字符串
///
/// 内容文本框,指定为 RichTextBox 文本框类型
/// 用户指定要查找的字符串
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;
}
}
/// 消息提示,提示用户查找结果
/// 参数1(str):用户指定要查找的字符串
/// 用户指定要查找的字符串
private void seeks(string str)
{
if (sun != 0)
MessageBox.Show(string.Format("已查找完毕,共〖{0}〗个 \"{1}\"!", sun, str), "查找 - 温馨提示");
else MessageBox.Show(string.Format("没有查找到 \"{0}\"!", str), "查找 - 温馨提示");
}
/// 全部替换指定〖字符 或 字符串〗
/// 参数1(rbox):要替换内容的文本框,指定为 RichTextBox 文本框类型
/// 参数2(str0):指定〖原有〗的内容(查找内容)
/// 参数3(str1):指定〖新〗的内容(替换内容)
/// 要替换内容的文本框,指定为 RichTextBox 文本框类型
/// 指定〖原有〗的内容(查找内容)
/// 指定〖新〗的内容(替换内容)
private void ReplaceAll(RichTextBox rbox, string str0, string str1)
{
rbox.Text = rbox.Text.Replace(str0, str1);
}
/// 单次替换字符或字符串
/// 参数1(str0):查找的内容
/// 参数2(str1):要替换的内容
///
/// 查找的内容
/// 要替换的内容
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;
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.button1.Enabled = true;
}