C# richtextbox查找和替换指定字符串

RichTextBox实现文本查找替换的功能

操作视图:

C# richtextbox查找和替换指定字符串_第1张图片

代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace 查找和替换指定字符串_

{

    public partial class Form1 : Form

    {

        publicForm1()

        {

            InitializeComponent();

            stringstr = "";

            richTextBox1.Text = str;

        }

        intstart = 0;

        intcount = 0;

        ///

        ///查找字符串

        ///

        ///

        ///

        privatevoid find_Click_Click(objectsender, EventArgs e)

        {

            stringstr1;

            str1 = txt_find.Text;

            if(start >= richTextBox1.Text.Length)

            {

                MessageBox.Show("已查找到尾部");

                start = 0;

            }

            else

            {

                start = richTextBox1.Find(str1,start, RichTextBoxFinds.MatchCase);

                if(start == -1)

                {

                    if(count == 0)

                    {

                        MessageBox.Show("没有该字符");

                    }

                    else

                    {

                        MessageBox.Show("以查找到尾部!");

                        start = 0;

                    }

                }

                else

                {

                    start = start +str1.Length;

                    richTextBox1.Focus();

                }

            }

        }

        privatevoid replace_Click_Click(object sender, EventArgse)

        {

            richTextBox1.Text =richTextBox1.Text.Replace(txt_find.Text, txt_replace.Text);

        }

        privatevoid txt_find_TextChanged(object sender, EventArgse)

        {

            stringstr1;

            str1 = txt_find.Text;

            start = 0;

            count = 0;

        }

    }

}

 

小结:

这些功能window上ctrl+f都实现了,所以没什么用。练练手还是可以的。

你可能感兴趣的:(C# richtextbox查找和替换指定字符串)