查找和替换

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 对话框
{
    public partial class Form2 : Form
    {

        public Form2()
        {
            InitializeComponent();//初始化窗体组件
        }
        private Form1 jieshou;
        public Form2(Form1 form1)//构造函数,接收Form1窗体
        {
            // TODO: Complete member initialization
            InitializeComponent();
            jieshou = form1;
        }
        private int a = -1;
        private int index;
        private int xuanzhong = 1;
        /// <summary>
        /// 查找函数
        /// </summary>
        public void chazhao(string cztxt)
        {
            
            if (xuanzhong == 1)//选择向上查找
            {
                if (a == -1)//初始值,第一次点击查找
                {
                    //查询cztxt在文本中的索引,也是为了判断文本中是否包含cztxt,如果查询不到
                    //indexof返回值为-1
                    index = jieshou.textBox1.Text.IndexOf(cztxt);
                    if (index != -1)//在不等于-1的情况下执行,也就是文本中包含cztxt
                    {
                        jieshou.textBox1.Select(index, cztxt.Length);
                        jieshou.textBox1.Focus();//为控件设置输入焦点,也就是聚焦显示选中的文本
                        a = index + 1;//索引加1,从匹配的第一个实例的下一位开始查询
                    }
                }
                else
                {
                    if (a + cztxt.Length < jieshou.textBox1.Text.Length-1)//为了保证查询的cztxt的长度没有超出
                    {
                        //查询cztxt在文本中的索引,也是为了判断文本中是否包含cztxt,如果查询不到
                        //indexof返回值为-1
                        index = jieshou.textBox1.Text.IndexOf(cztxt, a);
                        if (index != -1)
                        {
                            jieshou.textBox1.Select(index, cztxt.Length);
                            a = index + 1;
                        }
                    }
                    if (index==-1)
                    {
                        a = -1;//直到查询不到cztxt,把a初始化 从新开始查
                    }
                }
            }
            else//选择向下查询
            {
                
                if (a == -1)
                {
                    //查询cztxt在文本中的索引,也是为了判断文本中是否包含cztxt,如果查询不到
                    //indexof返回值为-1
                    index = jieshou.textBox1.Text.LastIndexOf(cztxt);
                    if (index != -1)
                    {                     
                        jieshou.textBox1.Select(index, cztxt.Length);
                        jieshou.textBox1.Focus();
                        a = index - 1;//索引减1,从匹配的第一个实例的上一位开始查询
                    }
                }
                else
                {
                    if (a-cztxt.Length>=-1)
                    {
                        //查询cztxt在文本中的索引,也是为了判断文本中是否包含cztxt,如果查询不到
                        //indexof返回值为-1
                        index = jieshou.textBox1.Text.LastIndexOf(cztxt, a);
                        if (index != -1)
                        {                           
                            jieshou.textBox1.Select(index, cztxt.Length);
                            a = index - 1;//索引减1,从匹配的第一个实例的上一位开始查询
                        }
                    }
                    if (index==-1)
                    {
                        a = -1;//直到查询不到cztxt,把a初始化 从新开始查
                    }
                }
            }
            jieshou.textBox1.Focus();//为控件设置输入焦点,也就是聚焦显示选中的文本
            
        }
        
        private void button1_Click_1(object sender, EventArgs e)
        {
            string cztxt = textBoxchazhao.Text;
            chazhao(cztxt);
        }
        
        private void xiangshang_CheckedChanged(object sender, EventArgs e)
        {
            xuanzhong = 1;//向上查询初值
        }

        private void xiangxia_CheckedChanged(object sender, EventArgs e)
        {
            xuanzhong = 2;//向下查询初值
        }

        private void button2_Click(object sender, EventArgs e)//替换
        {
            string cztxt = textBoxchazhao.Text;
            if (jieshou.textBox1.Text.Contains(cztxt))//判读文本中是否包含cztxt
            {
                chazhao(cztxt);
                //把选中的字符串替换掉
                jieshou.textBox1.SelectedText = textBoxtihuan.Text;
                jieshou.textBox1.Focus();
            }
        }
        /// <summary>
        /// 替换全部
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            while (true)
            {
                string cztxt = textBoxchazhao.Text;
                if (jieshou.textBox1.Text.Contains(cztxt))//判读文本中是否包含cztxt
                {
                    chazhao(cztxt);
                    //把选中的字符串替换掉
                    jieshou.textBox1.SelectedText = textBoxtihuan.Text;
                    jieshou.textBox1.Focus();
                }
                else//不包含cztxt就跳出循环
                {
                    break;
                }
            }
        }
    }
}

 

posted @ 2015-05-31 20:53  九省巡按  阅读(175)  评论(0编辑  收藏  举报