记事本(查找与替换)

namespace 查找
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)//查找
        {
            find f = new find(this); //将记事本窗体扔进去
            f.Show();//将窗体显示出来
        }
    }
}
namespace 查找
{
    public partial class find : Form
    {
        public find()
        {
            InitializeComponent();
        }
        private Form1 fuform;  //创建一个Form1类型的变量fuform用来接收 jishiben那个窗体
        public find(Form1 jishiben) //通过创建构造函数来实现查找窗体控制记事本窗体的内容
        {
            InitializeComponent();
            fuform = jishiben; //将扔进来的jishiben给这个变量

        }
      
        private int count; //定义这个变量是为了判断是第几次查找,如果是第一次查找count就为0;
        private void button1_Click(object sender, EventArgs e)
        {
            string cztxt = textchazhao.Text; //这个变量是查找框里的文本内容
            if (count == 0) //如果是第一次查找
            {
                int index = fuform.textBox1.Text.IndexOf(cztxt); //index代表的是查找框内容在记事本文本框中的索引 
                if(index!=-1)  //如果查到最后了 即便不能查了
                {
                    fuform.textBox1.Select(index,cztxt.Length);  //从索引开始,选中查找框里内容的长度
                    count = index + 1;//将count+1 是为了执行else, 即再点查找便不是第一次查找了,而是查找下一个
                    fuform.textBox1.Focus(); //将焦点返回记事本的文本框
                }
            }
            else
            {
                int index = fuform.textBox1.Text.IndexOf(cztxt,count);  //加上count是为了屏蔽掉之前已经查了的, index(x,y)
                {                                                       //的意思是从y开始查找x
                    fuform.textBox1.Select(index, cztxt.Length);
                    count = index + 1;
                    fuform.textBox1.Focus();
                }
            }
        }      
    }
}
private void button1_Click(object sender, EventArgs e)//替换
        {
            find f = new find(this); //将记事本窗体扔进去
            f.Show();//将窗体显示出来
        }
 
private void button2_Click(object sender, EventArgs e) //替换
        {
            string cztxt = textchazhao.Text;  //查找栏的文本
            string thtxt = texttihuan.Text; //替换栏的文本
            if (count == 0)
            {
                int index = fuform.textBox1.Text.IndexOf(cztxt);
                //MessageBox.Show(index.ToString());
                if (index != -1)
                {
                    fuform.textBox1.Select(index, cztxt.Length);
                    count = index + 1;
                    fuform.textBox1.SelectedText = thtxt;
                    fuform.textBox1.Focus();
                }
            }
            else
            {
                int index = fuform.textBox1.Text.IndexOf(cztxt);
                //MessageBox.Show(index.ToString()); //显示索引是多少
                if (index != -1)
                {
                    fuform.textBox1.Select(index, cztxt.Length);
                    count = index + 1;
                    fuform.textBox1.SelectedText = thtxt; //将选中的文本变成替换栏的文本
                    fuform.textBox1.Focus();
                }
            }
        }
原文地址:https://www.cnblogs.com/lk-kk/p/4541965.html