C#RichTextBox 文本查找与替换

〖欢迎转载〗转载请注明出处

把 查找,替换,全部替换 三个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;
        }


  • 1
    点赞
  • 18
    收藏 更改收藏夹
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茗香淡然

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值