`

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

    博客分类:
  • C#
 
阅读更多

一、程序入口:
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();
            }
        }
    }
}

查找、替换窗体


  • 大小: 26.7 KB
分享到:
评论

相关推荐

    给VB新手实例,包括TreeView.TabStrip等20个控件的使用

    6. ListBox和ComboBox控件:ListBox用于显示可选择的项目列表,用户可单选或多选。ComboBox则结合了TextBox和ListBox,用户可输入或从下拉列表中选择。 7. CheckBox和RadioButton控件:CheckBox用于提供多选选项,...

    C#控件查询手册

    ListBox、ListView和TreeView控件则提供了不同形式的列表展示。ListView控件尤其强大,它支持以多种视图(图标、列表或详细信息)展示数据,并且可以对项进行分组。 **图形显示和存储控件** PictureBox控件用于在...

    C#控件大全(C#所有的控件)

    C#控件大全是指C#语言中提供的所有控件的集合,包括窗体、按钮、文本框、列表框、组合框、checkbox、RadioButton、 label、ProgressBar、TextBox、RichTextBox、DataGridView、ListView、TreeView等。这些控件都是C#...

    VB中各控件模块超详细介绍大全及各控件比较

    - **ListBox**和**ComboBox**:列表选择控件,ListBox展示固定列表,ComboBox可下拉选择并允许输入。 2. **容器控件**: - **Form**:程序的主要窗口,承载其他控件。 - **GroupBox**:用于将一组相关的控件分组...

    c# 控件大全

    - **RichTextBox 控件**: - 功能:支持更复杂的文本格式化和布局,如字体、颜色、对齐方式等。 - 应用场景:适合于需要展示富文本内容的应用。 - **MaskedTextBox 控件**: - 功能:限制用户输入特定格式的数据...

    C#控件操作源码_透明控件_弹出提醒窗体_进度条带数字_根据文件大小设置进度条_美化各种控件_水晶按钮等

    DataGridView操作 DisplayRowCount EnglishTextBox FileBatchCopy GetClipBoardPicture PlanText Popup窗口提醒 RestrictDigit Vista风格的日历 带行数和标尺的RichTextBox 弹出模式窗口显示进度条 根据文件大小显示...

    C#控件查询手册 (超详细)

    - **数据绑定**:将数据源绑定到如`DataGridView`、`ComboBox`等控件上。 **示例代码** ```csharp public Form1() { InitializeComponent(); BindingSource bindingSource1 = new BindingSource(); // 创建 ...

    C#控件命名规范

    根据文档提供的内容,我们将对以下几种类型的控件进行详细的命名规则说明: 1. **数据显示控件** - **DataGridView**: `dgv` + 控件含义 - **BindingSource**: `bds` + 控件含义 - **BindingNavigator**: `bdn` ...

    王牌2_C#_控件查询手册.pdf

    - **查找替换**:提供文本查找和替换功能。 - **保存和加载**:支持保存和加载文本文件。 **3.3 MaskedTextBox控件** `MaskedTextBox`控件允许用户按照指定的掩码输入文本,常用于输入电话号码、日期等有固定格式...

    C#控件查询手册

    - 行/列操作:支持动态添加、删除行列以及对行列进行排序、筛选等功能。 #### 二、数据绑定和定位控件 ##### BindingSource组件 - **定义与功能**:`BindingSource`组件用于简化数据绑定的过程,它充当数据源和...

    C#控件前缀名大全

    在C#编程语言中,控件是构建图形用户界面(GUI)的核心元素,它们负责接收用户的输入、显示信息以及执行各种交互操作。为了方便管理和识别,C#中的控件通常会采用特定的前缀名来命名,这不仅有助于提高代码的可读性...

    windows窗体控件

    6. **RichTextBox控件**:扩展了TextBox控件的功能,支持富文本格式,如字体样式、颜色等。 - **特点**:支持复杂的文本格式;可以保存为RTF格式。 7. **MaskedTextBox控件**:允许用户根据预设的掩码输入数据,...

    C# 控件缩写大全

    1. **dts (DataSet)**:数据集控件,存储和操作数据集合。 2. **dgv (DataGridView)**:数据网格控件,以表格形式展示数据。 3. **bds (BindingSource)**:绑定源控件,作为数据绑定的中介。 4. **bdn ...

    C#控件大全

    从列表中选择控件,如CheckedListBox、ComboBox、DomainUpDown、ListBox、ListView和NumericUpDown等,这些控件提供了丰富的选择功能,适用于不同的用户界面场景。例如,CheckedListBox提供带有复选框的列表项供用户...

    C#控件简写,规范代码

    此外,还有更多如AdRotator、Column(DataGridView)、ColumnHeader(ListView)、ComboBox、CompareValidator、CrystalReportViewer等控件,它们的简写形式如文中所示。掌握这些简写,能有效提升C#编程的速度和效率...

    C#控件缩写大全

    以下是对标题和描述中涉及的一些C#控件及其缩写的详细解释: 1. **btn Button**: Button 控件是最基本的控件之一,用于触发一个事件,比如点击按钮执行某项操作。 2. **chk CheckBox**: CheckBox 控件让用户可以...

    C# 常用控件查询手册.PDF

    在.NET框架下,C#是一种广泛使用的编程语言,尤其在开发Windows桌面应用程序时,它提供了丰富的控件库,使得开发者可以构建用户界面。本“C#常用控件查询手册”聚焦于这些控件,帮助开发者理解和掌握它们的功能、...

    c#文档和控件

    #### 一、DataGridView控件 - **定义**:`DataGridView`控件是一种用于在Windows Forms应用程序中展示和编辑数据的强大工具。它支持各种数据源,如数据库表、数据集、数据视图等,并提供丰富的功能来处理数据。 - **...

Global site tag (gtag.js) - Google Analytics