对集合控件(ListBox,ComboBox,TreeView,RichTextBox,DataGridView)的查找、替换操作控件


一、程序入口:
using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;
namespace HR.Control
{
    /// <summary>
    /// 对集合类控件中的文本进行查找、替换的组件
    /// </summary>
    public class SearchSelect
    {
        /// <summary>
        /// 查找文件的操作类型
        /// </summary>
        public enum FindOption
        {
            /// <summary>
            /// 查找文本
            /// </summary>
            FindText,
            /// <summary>
            /// 替换文本
            /// </summary>
            ReplaceText
        }


        /// <summary>
        /// 查找ListBox控件
        /// </summary>
        /// <param name="lb">ListBox</param>
        /// <param name="option">查找类型</param>
        /// <remarks></remarks>
        public void FindAndSelect(ListBox lb, FindOption option)
        {
            LoadForm(lb, option, "ListBox");
        }

        /// <summary>
        /// 查找ComboBox控件
        /// </summary>
        /// <param name="cbb">ComboBox</param>
        /// <param name="option">查找类型</param>
        /// <remarks></remarks>
        public void FindAndSelect(ComboBox cbb, FindOption option)
        {
            LoadForm(cbb, option, "ComboBox");
        }


        /// <summary>
        /// 查找TreeView控件
        /// </summary>
        /// <param name="tv">TreeView</param>
        /// <param name="option">查找类型</param>
        /// <remarks></remarks>
        public void FindAndSelect(TreeView  tv, FindOption option)
        {
            LoadForm(tv, option, "TreeView");
        }

        /// <summary>
        /// 查找RichTextBox控件
        /// </summary>
        /// <param name="rtb">RichTextBox</param>
        /// <param name="option">查找类型</param>
        /// <remarks></remarks>
        public void FindAndSelect(RichTextBox rtb, FindOption option)
        {
            LoadForm(rtb, option, "RichTextBox");
        }


        /// <summary>
        /// 查找DataGridView控件
        /// </summary>
        /// <param name="dgv">DataGridView</param>
        /// <param name="option">查找类型</param>
        /// <remarks></remarks>
        public void FindAndSelect(DataGridView  dgv, FindOption option)
        {
            LoadForm(dgv, option, "DataGridView");
        }

        /// <summary>
        /// 加载窗体,根据不同的控件
        /// </summary>
        /// <param name="control">要查找的控件</param>
        /// <param name="option">查找类型</param>
        /// <param name="controlType">控件类型</param>
        private static void LoadForm(object control, FindOption option,string controlType)
        {
            HR.Control.Form1_Search fs = new Form1_Search();
            fs.FindControl = control;
            fs.ControlType = controlType;
            if (option == FindOption.FindText)//查找文本
            {
                fs.tabControl1.SelectedTab = fs.tabFind;
            }
            else
            {
                fs.tabControl1.SelectedTab = fs.tabReplace;
            }
            fs.ShowDialog();
        }
    }
}


二、入口程序调用的窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HR.Control
{
    public partial class Form1_Search : Form 
    {
        /// <summary>
        /// 要查找的控件
        /// </summary>
        public object FindControl;
        /// <summary>
        /// 查找的控件类型
        /// </summary>
        public string ControlType;

        /// <summary>
        ///选择的索引
        /// </summary>
        int selIndex;

        /// <summary>
        /// 选择的行
        /// </summary>
        int selR;
        
        /// <summary>
        /// 选择的列
        /// </summary>
        int selC;

        /// <summary>
        /// 开始索引
        /// </summary>
        int sI;
        /// <summary>
        /// 结束索引
        /// </summary>
        int eI;
        public Form1_Search()
        {
            InitializeComponent();
        }

        private void Form1_Search_Load(object sender, EventArgs e)
        {
            switch (ControlType)
            {
                case"ListBox":
                    this.btnColor.Enabled = false;
                    this.checkBox4.Enabled = false;
                    ListBox lb = (ListBox)FindControl;
                    if (lb.Items.Count==0)
                    {
                        MessageBox.Show("没有可查找的项,查找将终止!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    if (lb.SelectionMode==SelectionMode.One)
                    {
                        this.btnFindAll.Enabled = false;
                    };
                    break;


                case"ComboBox":
                    this.btnColor.Enabled = false;
                    this.checkBox4.Enabled = false;
                    ComboBox cbb = (ComboBox)FindControl;
                    if (cbb.Items.Count==0)
                    {
                         MessageBox.Show("没有可查找的项,查找将终止!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                         this.Close();
                    };
                    this.btnFindAll.Enabled = false;
                    break;

                case"TreeView":
                    this.btnColor.Enabled = false;
                    this.checkBox4.Enabled = false;
                    TreeView tv = (TreeView)FindControl;
                    if (tv.Nodes.Count==0)
                    {
                           MessageBox.Show("没有可查找的项,查找将终止!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                         this.Close();
                    }
                    this.btnfindNext.Enabled = false;
                    break;

                case"RichTextBox":
                    RichTextBox rtb = (RichTextBox)FindControl;
                    if (string.IsNullOrEmpty(rtb.Text))
                    {
                          MessageBox.Show("没有可查找的项,查找将终止!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                         this.Close();
                    }
                    this.ckbsbcordbc.Enabled = false;
                    this.chkTpf.Enabled = false;
                    break;

                case"DataGridView":
                    DataGridView dgv = (DataGridView)FindControl;
                    if (dgv.Rows.Count == 0)
                    {
                        MessageBox.Show("没有可查找的项,查找将终止!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    };
                    break;
                 
            }
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 对ListBox的查找、替换操作
        /// </summary>
        /// <param name="lb"></param>
        /// <param name="lx"></param>
        private void FindListBox(ListBox lb, string lx)
        {
            if (lx == "Next")
            {
                if (lb.SelectedIndex == -1)
                {
                    selIndex = 0;
                }
                else
                {

                    if (selIndex == 0)
                    {
                        selIndex = lb.SelectedIndex;
                    }
                }

                string tempFindText =null;
                string tempDesc = null;
                int i;
                if (this.tabControl1.SelectedTab.Text == "查找")
                {
                    tempFindText = this.txtFindText.Text;
                }
                else
                {
                    tempFindText = this.txtFindTextB.Text;
                }
                //从选中的索引位置开始找
                for (i = selIndex; i < lb.Items.Count; i++)
                {
                    tempDesc = lb.Items[i].ToString();
                    if (this.ckbsbcordbc.Checked == false)  //不区分全角和半角
                    {
                        HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
                        tempDesc = SO.SBCToDBC(lb.Items[i].ToString());
                        tempFindText = SO.SBCToDBC(tempFindText);
                    }
                    if (this.ckbUpporLow.Checked == false) //不区分大小写
                    {
                        tempDesc = tempDesc.ToLower();
                        tempFindText = tempFindText.ToLower();
                    }
                    if (this.chkTpf.Checked == true) //使用通配符
                    {
                        if (tempDesc.IndexOf(tempFindText) > -1) //有匹配项
                        {
                            if (this.tabControl1.SelectedTab.Text == "替换")
                            {

                                lb.Items[i] = tempDesc.Replace(tempFindText,this.txtFindReplaceText.Text);
                            }
                            lb.SelectedIndex = i;
                            selIndex = i + 1;
                            break;
                        }
                    }
                    else  //不使用通配符
                    {
                        if (tempDesc.IndexOf(tempFindText) == 0)
                        {
                            if (this.tabControl1.SelectedTab.Text == "替换")
                            {
                                lb.Items[i] = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
                            }
                            lb.SelectedIndex = i;
                            selIndex = i + 1;
                            break;
                        }
                    }
                }

                //已经查找到最后一项
                if (i == lb.Items.Count)
                {
                    lb.SelectedIndex = -1;
                }
            }
            else //全部查找
            {
                string tempFindText =null;
                string tempDesc = null;
                int i = 0;
                if (this.tabControl1.SelectedTab.Text == "查找")
                {
                    tempFindText = this.txtFindText.Text;
                }
                else
                {
                    tempFindText = this.txtFindTextB.Text;
                }
                for (i = 0; i <lb.Items.Count; i++)
                {
                    if (this.ckbsbcordbc.Checked == false)
                    {
                        HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
                        tempDesc = SO.SBCToDBC(lb.Items[i].ToString());
                        tempFindText = SO.SBCToDBC(tempFindText);
                    }
                    if (this.ckbUpporLow.Checked == false)
                    {
                        tempFindText = tempFindText.ToLower();
                        tempDesc = tempDesc.ToLower();
                    }
                    if (this.chkTpf.Checked == true)
                    {
                        if (this.tabControl1.SelectedTab.Text == "替换")
                        {
                            if (tempDesc.IndexOf(tempFindText) > -1)
                            {
                                lb.Items[i] = tempDesc.Replace(this.txtFindTextB.Text, this.txtFindReplaceText.Text);
                            }
                            lb.SelectedIndex = i;
                        }
                    }
                    else
                    {
                        if (tempDesc.IndexOf(tempFindText) == 0)
                        {
                            if (this.tabControl1.SelectedTab.Text == "替换")
                            {
                                lb.Items[i] = tempDesc.Replace(this.txtFindTextB.Text, this.txtFindReplaceText.Text);
                            }
                            lb.SelectedIndex = i;
                        }
                    }
                }
            }
        }


        /// <summary>
        /// 对ComboBox控件的查找、替换操作
        /// </summary>
        /// <param name="cmm"></param>
        /// <param name="lx"></param>
        private void FindComboBox(ComboBox cmm,string lx)
        {
            string tempDesc = null;
            string tempFindText = null;
          
            if (cmm.SelectedIndex==-1)
            {
                selIndex = 0;
            }
            if (this.tabControl1.SelectedTab.Text == "替换")
            {
                tempFindText = this.txtFindTextB.Text;
            }
            else
            {
                tempFindText = this.txtFindText.Text;
            }
            int i;
            for (i = selIndex; i < cmm.Items.Count; i++)
            {
                tempDesc = cmm.Items[i].ToString();
                if (this.ckbsbcordbc.Checked==false)
                {
                    HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
                   tempDesc = SO.SBCToDBC(tempDesc);
                   tempFindText = SO.SBCToDBC(tempFindText);
                }
                if (this.ckbUpporLow.Checked==false)
                {
                    tempDesc = tempDesc.ToLower();
                    tempFindText = tempFindText.ToLower();
                }
                if (this.chkTpf.Checked == true) //使用通配符
                {
                    if (tempDesc.IndexOf(tempFindText) > -1)
                    {
                        if (this.tabControl1.SelectedTab.Text == "替换")
                        {
                            cmm.Items[i] = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
                        }
                        cmm.SelectedIndex = i;
                        selIndex = selIndex + 1;
                        break;
                    }
                }
                else //使用通配符
                {
                    if (tempDesc.IndexOf(tempFindText)==0)
                    {
                        if (this.tabControl1.SelectedTab.Text == "替换")
                        {

                            cmm.Items[i] = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
                        }
                        cmm.SelectedIndex = i;
                        selIndex = selIndex + 1;
                        break;
                        
                    }
                }
            }

            if (i==cmm.Items.Count)
            {
                cmm.SelectedIndex = -1;
            }
        }


        /// <summary>
        /// 对RichTextBox控件的查找、替换操作
        /// </summary>
        /// <param name="rct"></param>
        /// <param name="lx"></param>
        private void FindRichTextBox(RichTextBox rct, string lx)
        {
            
            eI = rct.Text.Length;
            int i = 0;
            string tempFindText = null;
            if (sI==0)
            {
                sI = 0;
            }

            try
            {
                if (lx == "Next")
                {
                    if (this.tabControl1.SelectedTab.Text == "替换")
                    {
                        tempFindText = this.txtFindTextB.Text;
                        if (this.ckbUpporLow.Checked == true) //区分大小写
                        {
                            i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.MatchCase);
                        }
                        else
                        {
                            i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.None);
                        }
                        if (i > 0)//找到了匹配项
                        {
                            rct.SelectedText = this.txtFindReplaceText.Text;
                            rct.SelectionStart = i;
                            rct.SelectionLength = txtFindReplaceText.Text.Length;//从新定位一个新的位置开始找
                            sI = i + txtFindReplaceText.Text.Length;
                        }

                    }
                    else //查找
                    {
                        tempFindText = this.txtFindText.Text;
                        if (this.ckbUpporLow.Checked == true) //区分大小写
                        {
                            i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.MatchCase);
                        }
                        else
                        {
                            i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.None);
                        }
                        sI = i + tempFindText.Length;

                    }
                }
                else //查找全部
                {
                    sI = 0;
                    eI = rct.Text.Length;
                    if (this.tabControl1.SelectedTab.Text == "替换")
                    {
                        tempFindText = this.txtFindTextB.Text;
                        int k = 0;
                        for (k = 0; k < rct.Text.Length; k++)
                        {
                            if (this.ckbUpporLow.Checked == true)
                            {
                                i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.MatchCase);
                            }
                            else
                            {
                                i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.None);
                            }
                            if (i > 0)
                            {
                                rct.SelectedText = this.txtFindReplaceText.Text;
                                rct.SelectionStart = i;
                                rct.SelectionLength = txtFindReplaceText.Text.Length;//从替换后的新位置开始找
                                sI = i + txtFindReplaceText.Text.Length;
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                    else //如果是查找
                    {
                        rct.SelectionColor = Color.Black;
                        tempFindText = this.txtFindText.Text;
                        int k = 0;
                        for (k = 0; k < rct.Text.Length; k++)
                        {
                            if (this.ckbUpporLow.Checked == true)
                            {
                                i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.MatchCase);
                            }
                            else
                            {
                                i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.None);
                            }
                            if (i > 0)
                            {
                                sI = i + tempFindText.Length;
                                rct.SelectionColor = this.btnColor.BackColor;
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                }
            }
            catch
            {
                sI = 0;
            }
            
        }

        /// <summary>
        /// 对TreeView的查找、替换操作
        /// </summary>
        /// <param name="tv">TreeView控件</param>
        /// <param name="lx"></param>
        /// <remarks></remarks>
        private void FindTreeView(TreeView  tv, string lx)
        {
            foreach (TreeNode  FirstNode in tv.Nodes)
            {
                RecurseTreeNode(FirstNode);   
            }
        }

        /// <summary>
        /// 递归查找树节点
        /// </summary>
        /// <param name="tempNode">树节点</param>
        private void RecurseTreeNode(TreeNode tempNode)
        {
           tempNode.ForeColor = Color.Black;
            tempNode.Expand();
            string tempFindText = "";
            string tempDesc = "";
            if (this.tabControl1.SelectedTab.Text == "替换")
            {
                tempFindText = this.txtFindTextB.Text;
            }
            else
            {
                tempFindText = this.txtFindText.Text;
            }

            tempDesc = tempNode.Text;
            if (this.ckbsbcordbc.Checked==false)
            {
                HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
                tempDesc = SO.SBCToDBC(tempDesc);
                tempFindText = SO.SBCToDBC(tempFindText);
            }
            if (this.ckbUpporLow.Checked==false)
            {
                   tempDesc = tempDesc.ToLower();
                   tempFindText = tempFindText.ToLower();
            }
            if (this.chkTpf.Checked == true)
            {
                if (tempDesc.IndexOf(tempFindText) > -1)
                {
                    tempNode.ForeColor = this.btnColor.BackColor;
                    if (this.tabControl1.SelectedTab.Text == "替换")
                    {
                        tempNode.Text = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
                    }
                    if (tempNode.Parent != null) //当前节点有父节点时,展开父节点
                    {
                        tempNode.Parent.Expand();
                    }
                }
            }
            else
            {
                if (tempDesc.IndexOf(tempFindText)==0)
                {
                     tempNode.ForeColor = this.btnColor.BackColor;
                     if (this.tabControl1.SelectedTab.Text == "替换 ")
                     {
                         tempNode.Text = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
                     }
                     if (tempNode.Parent != null) //当前节点有父节点时,展开父节点
                     {
                         tempNode.Parent.Expand();
                     }
                }
            }

            foreach (TreeNode  aNode in tempNode.Nodes)
            {
                RecurseTreeNode(aNode);
            }
        }


        /// <summary>
        /// 对DataGridView的查找、替换操作
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="lx"></param>
        private void FindDataGridView(DataGridView  dgv, string lx)
        {
            int i = 0;
            int j = 0;
            string tempDesc = null;
            string tempFindText = null;
            if (this.tabControl1.SelectedTab.Text == "替换")
            {
                tempFindText= this.txtFindText.Text;
            }
            else
            {
                tempFindText = this.txtFindTextB.Text;
            }

            if (lx == "Next")
            {
                for (i = selR; i < dgv.Rows.Count; i++)
                {
                    for (j = selC; j < dgv.ColumnCount; j++)
                    {
                        selC = j + 1;

                        dgv.Rows[i].Cells[j].Style.ForeColor = Color.Black;

                        if (!Object.ReferenceEquals(dgv.Rows[i].Cells[j].Value, null))  //单元格不为空
                        {
                            tempDesc = dgv.Rows[i].Cells[j].Value.ToString();

                            if (this.ckbsbcordbc.Checked == false)
                            {
                                HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
                                tempDesc = SO.SBCToDBC(tempDesc);
                                tempFindText = SO.SBCToDBC(tempFindText);
                            }
                            if (this.ckbUpporLow.Checked == false)
                            {
                                tempDesc = tempDesc.ToLower();
                                tempFindText = tempFindText.ToLower();
                            }
                            if (this.chkTpf.Checked == true)
                            {
                                if (tempDesc.IndexOf(tempFindText) > -1)
                                {
                                    if (this.tabControl1.SelectedTab.Text == "替换")
                                    {
                                        dgv.Rows[i].Cells[j].Value = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
                                    }
                                    dgv.Rows[i].Cells[j].Selected = true;
                                    dgv.Rows[i].Cells[j].Style.ForeColor = btnColor.BackColor;
                                    dgv.FirstDisplayedCell = dgv.Rows[i].Cells[j];//设置查找到的单元格,一定出现在可视区域
                                    return;
                                }
                            }
                            else
                            {
                                if (tempDesc.IndexOf(tempFindText) == 0)
                                {
                                    if (this.tabControl1.SelectedTab.Text == "替换")
                                    {
                                        dgv.Rows[i].Cells[j].Value = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
                                    }
                                    dgv.Rows[i].Cells[j].Selected = true;
                                    dgv.Rows[i].Cells[j].Style.ForeColor = btnColor.BackColor;
                                    dgv.FirstDisplayedCell = dgv.Rows[i].Cells[j];//设置查找到的单元格,一定出现在可视区域
                                    return;
                                }
                            }

                        }
                    }
                    //当一行查找完后
                    if (selC == dgv.ColumnCount)
                    {
                        selR = selR + 1;
                        selC = 0;
                    }
                }
                //所有行查找完后
                if (selR == dgv.Rows.Count)
                {
                    selR = 0;
                    selC = 0;
                }
            }
            else  //查找全部
            {
                for (i = selR; i < dgv.Rows.Count; i++)
                {
                    for (j = selC; j < dgv.ColumnCount; j++)
                    {
                       dgv.Rows[i].Cells[j].Style.ForeColor = Color.Black;

                        if (!Object.ReferenceEquals(dgv.Rows[i].Cells[j].Value, DBNull.Value))  //单元格不为空
                        {
                            tempDesc = dgv.Rows[i].Cells[j].Value.ToString();

                            if (this.ckbsbcordbc.Checked == false) //不区分全角半角
                            {
                                HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
                                tempDesc = SO.SBCToDBC(tempDesc);
                                tempFindText = SO.SBCToDBC(tempFindText);
                            }
                            if (this.ckbUpporLow.Checked == false)//不区分大小写
                            {
                                tempDesc = tempDesc.ToLower();
                                tempFindText = tempFindText.ToLower();
                            }
                            if (this.chkTpf.Checked == true)
                            {
                                if (tempDesc.IndexOf(tempFindText) > -1)
                                {
                                    if (this.tabControl1.SelectedTab.Text == "替换")
                                    {
                                        dgv.Rows[i].Cells[j].Value = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
                                    }
                                    dgv.Rows[i].Cells[j].Selected = true;
                                    dgv.Rows[i].Cells[j].Style.ForeColor = btnColor.BackColor;
                                    dgv.FirstDisplayedCell = dgv.Rows[i].Cells[j];//设置查找到的单元格,一定出现在可视区域
                                }
                            }
                            else
                            {
                                if (tempDesc.IndexOf(tempFindText) == 0)
                                {
                                    if (this.tabControl1.SelectedTab.Text == "替换")
                                    {
                                        dgv.Rows[i].Cells[j].Value = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
                                    }
                                    dgv.Rows[i].Cells[j].Selected = true;
                                    dgv.Rows[i].Cells[j].Style.ForeColor = btnColor.BackColor;
                                    dgv.FirstDisplayedCell = dgv.Rows[i].Cells[j];//设置查找到的单元格,一定出现在可视区域
                                }
                            }

                        }
                    }
                }
            }
        
        }
        private void btnfindNext_Click(object sender, EventArgs e)
        {
            if (this.tabControl1.SelectedTab.Text == "查找" && string.IsNullOrEmpty(this.txtFindText.Text))
            {
                MessageBox.Show("请先输入要查找的内容,再进行查找!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if(this.tabControl1.SelectedTab.Text == "替换" && string.IsNullOrEmpty(this.txtFindTextB.Text))
            {
                MessageBox.Show("请先输入要查找的内容,再进行替换!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            switch (ControlType)
            {
                case"ListBox":
                    FindListBox((ListBox)FindControl, "Next");
                    break;

                case"ComboBox":
                    FindComboBox((ComboBox)FindControl, "Next");
                    break;

                case "RichTextBox":
                    FindRichTextBox((RichTextBox)FindControl, "Next");
                    break;

                case "TreeView":
                    FindTreeView((TreeView)FindControl, "Next");
                    break;

                case "DataGridView":
                    FindDataGridView((DataGridView)FindControl, "Next");
                    break;
            }
        }

        private void btnFindAll_Click(object sender, EventArgs e)
        {
            if (this.tabControl1.SelectedTab.Text == "查找" && string.IsNullOrEmpty(this.txtFindText.Text))
            {
                MessageBox.Show("请先输入要查找的内容,再进行查找!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (this.tabControl1.SelectedTab.Text == "替换" && string.IsNullOrEmpty(this.txtFindTextB.Text))
            {
                MessageBox.Show("请先输入要查找的内容,再进行替换!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            switch (ControlType)
            {
                case "ListBox":
                    FindListBox((ListBox)FindControl, "All");
                    break;

                case "ComboBox":
                    FindComboBox((ComboBox)FindControl, "All");
                    break;

                case "RichTextBox":
                    FindRichTextBox((RichTextBox)FindControl, "All");
                    break;

                case "TreeView":
                    FindTreeView((TreeView)FindControl, "All");
                    break;

                case "DataGridView":
                    FindDataGridView((DataGridView)FindControl, "All");
                    break;
            }
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            ColorDialog cd= new ColorDialog();
            if (cd.ShowDialog()==DialogResult.OK)
            {
                this.btnColor.BackColor = cd.Color;
            }
            if (this.tabControl1.SelectedTab.Text == "查找")
            {
                this.txtFindText.Focus();

            }
            else
            {
                this.txtFindTextB.Focus();
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (object.ReferenceEquals(this.tabControl1.SelectedTab,this.tabFind))
            {
                this.btnfindNext.Text = "查找下一处(&N)";
                this.btnFindAll.Text = "查找全部(&A)";
                this.txtFindText.Focus();
            }
            else
            {
                this.btnfindNext.Text = "替换下一处(&N)";
                this.btnFindAll.Text = "替换全部(&A)";
                this.txtFindTextB.Focus();
            }
        }
    }
}

查找、替换窗体
对集合控件(ListBox,ComboBox,TreeView,RichTextBox,DataGridView)的查找、替换操作控件

你可能感兴趣的:(datagridview)