zoukankan      html  css  js  c++  java
  • 记事本(查找与替换)

    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();
                    }
                }
            }
  • 相关阅读:
    OpenWrt arp 命令发布
    [Cocos2d-x]Cocos2d-x 3.2 学习笔记
    智课雅思词汇---二十四、名词性后缀ary(也是形容词后缀)
    算法讲课---1、贪心
    智课雅思词汇---二十三、名词性后缀mony
    js进阶---12-11、jquery如何给动态创建出来的元素绑定事件
    js进阶---12-12、jquery事件委托怎么使用
    js进阶---12-10、jquery绑定事件和解绑事件是什么
    新东方雅思词汇---7.3、dioxide
    对啊英语音标---四、双元音常见的字母发音组合有哪些
  • 原文地址:https://www.cnblogs.com/lk-kk/p/4541965.html
Copyright © 2011-2022 走看看