1. 程式人生 > >C#RichTextBox 文字查詢與替換

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;
        }