爱拼才会赢

以下代码都亲自测试过,可靠、可行。如果有错误欢迎批评指出!

C# TreeView 节点查找

主窗体部分代码

  /// <summary>
        /// 遍历树节点,并将节点存入List<TreeNode>集合中
        /// </summary>
        /// <param name="node"></param>
        /// <param name="nodeList"></param>
        public void GetAllNodes(TreeNodeCollection nodeCollection, List<TreeNode> nodeList)
        {

            foreach (TreeNode itemNode in nodeCollection)
            {
                nodeList.Add(itemNode);
                GetAllNodes(itemNode.Nodes, nodeList);
            }
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDir_Click(object sender, EventArgs e)
        {
            if (this.OwnedForms.Length > 0)
            {
                this.OwnedForms[0].Close();     //如果查询窗体已打开,则关闭已打开的窗口
            }
            DocParMarksQuery frm = new DocParMarksQuery(this);
            frm.ShowInTaskbar = false;
            frm.Show(this);
        }

 

查找窗体代码

 

  private DocParMarks parentFrm;
        private string lastFindString;  //记录上一次查找的文本
        private int lastFindCount;      //记录上一次查找的标记
        private List<TreeNode> nodeList = new List<TreeNode>();     //记录所有的树节点
        public TreeNode node = null;        //记录查找到的节点
        private bool IsUp = false;    //是否向上查找

        public DocParMarksQuery()
        {
            InitializeComponent();
        }

        public DocParMarksQuery(DocParMarks frm)
        {
            InitializeComponent();
            parentFrm = frm;
        }

        #region Methods
        /// <summary>
        /// 模糊匹配(向下查找)
        /// </summary>
        /// <param name="inputText"></param>
        /// <returns></returns>
        private int FindNodePartDown(string inputText, int startCount)
        {
            nodeList.Clear();
            parentFrm.GetAllNodes(parentFrm.tvDocType.Nodes, nodeList);
            for (int i = startCount; i < nodeList.Count; i++)
            {
                if (nodeList[i].Text.Contains(inputText))
                {
                    return i;
                }
            }


            return -1;
        }


        /// <summary>
        /// 模糊匹配(向上查找)
        /// </summary>
        /// <param name="inputText"></param>
        /// <returns></returns>
        private int FindNodePartUp(string inputText, int startCount)
        {
            nodeList.Clear();
            parentFrm.GetAllNodes(parentFrm.tvDocType.Nodes, nodeList);
            if (startCount == 0)
            {
                startCount = nodeList.Count - 1;
            }
            //for (int i = nodeList.Count - 1; i > startCount; i--)
            for (int i = startCount; i >= 0; i--)
            {
                if (nodeList[i].Text.Contains(inputText))
                {
                    return i;
                }
            }


            return -2;
        }

        /// <summary>
        /// 全字匹配查找(向下查找)
        /// </summary>
        /// <param name="inputText">查找的内容</param>
        /// <param name="startCount">起始位置</param>
        /// <returns></returns>
        private int FindNodeAllDown(string inputText, int startCount)
        {
            nodeList.Clear();
            //List<TreeNode> nodeList = new List<TreeNode>();
            parentFrm.GetAllNodes(parentFrm.tvDocType.Nodes, nodeList);
            for (int i = startCount; i < nodeList.Count; i++)
            {
                if (nodeList[i].Text.CompareTo(inputText) == 0)
                {
                    return i;
                }
            }
            return -1;
        }

        /// <summary>
        /// 全字匹配查找(向上查找)
        /// </summary>
        /// <param name="inputText">查找的内容</param>
        /// <param name="startCount">起始位置</param>
        /// <returns></returns>
        private int FindNodeAllUp(string inputText, int startCount)
        {
            nodeList.Clear();
            //List<TreeNode> nodeList = new List<TreeNode>();
            parentFrm.GetAllNodes(parentFrm.tvDocType.Nodes, nodeList);
            for (int i = nodeList.Count - 1; i > startCount; i--)
            {
                if (nodeList[i].Text.CompareTo(inputText) == 0)
                {
                    return i;
                }
            }
            return -1;
        }
        #endregion

        #region Events
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DocTypeQueryFrm_Load(object sender, EventArgs e)
        {
            this.Icon = new System.Drawing.Icon(AppDomain.CurrentDomain.BaseDirectory + @"Logo/Logo.ico");
        }


        /// <summary>
        /// 当文本框不为空时启用查找按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtInputText_TextChanged(object sender, EventArgs e)
        {
            if (txtInputText.Text.Trim() == "")
            {
                btnQueryNext.Enabled = false;
            }
            else
            {
                btnQueryNext.Enabled = true;
            }
        }


        /// <summary>
        /// 查找下一个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQueryNext_Click(object sender, EventArgs e)
        {
            string inputText = txtInputText.Text.ToString();        //输入框的值

            if (checkWord.Checked == true)          //判断是否全字匹配
            {
                if (rbtnUp.Checked == true)
                {
                    if (inputText.CompareTo(lastFindString) != 0 || (!IsUp))       //判断是否与上一次的查询文本相同
                    {
                        lastFindCount = FindNodeAllUp(inputText, 0);      //不同则重新查找
                        lastFindString = inputText;
                    }
                    else
                    {
                        lastFindCount = FindNodeAllUp(inputText, lastFindCount - 1);  //相同则从下一个节点开始查找
                        lastFindString = inputText;
                    }
                    IsUp = true;
                }
                else
                {

                    if (inputText.CompareTo(lastFindString) != 0 || (IsUp))       //判断是否与上一次的查询文本相同
                    {
                        lastFindCount = FindNodeAllDown(inputText, 0);      //不同则重新查找
                        lastFindString = inputText;
                    }
                    else
                    {
                        lastFindCount = FindNodeAllDown(inputText, lastFindCount + 1);  //相同则从下一个节点开始查找
                        lastFindString = inputText;
                    }
                    IsUp = false;
                }
            }
            else
            {
                if (rbtnUp.Checked == true)
                {

                    if (inputText.CompareTo(lastFindString) != 0 || (!IsUp))
                    {
                        lastFindCount = FindNodePartUp(inputText, 0);
                        lastFindString = inputText;
                    }
                    else
                    {
                        lastFindCount = FindNodePartUp(inputText, lastFindCount - 1);
                        lastFindString = inputText;
                    }
                    IsUp = true;
                }
                else
                {

                    if (inputText.CompareTo(lastFindString) != 0 || (IsUp))
                    {
                        lastFindCount = FindNodePartDown(inputText, 0);
                        lastFindString = inputText;
                    }
                    else
                    {
                        lastFindCount = FindNodePartDown(inputText, lastFindCount + 1);
                        lastFindString = inputText;
                    }
                    IsUp = false;
                }
            }
            if (lastFindCount == -1)        //判断是否找到节点
            {

                CommonApp.FormMessageBox.Show(CommonApp.LoadMode.Prompt, "查找到尾部,未找到指定的节点!");
                this.Focus();
                lastFindCount = 0;
                lastFindString = "";
                return;
            }
            else if (lastFindCount == -2)        //判断是否找到节点
            {

                CommonApp.FormMessageBox.Show(CommonApp.LoadMode.Prompt, "查找到顶部,未找到指定的节点!");
                this.Focus();
                lastFindCount = 0;
                lastFindString = "";
                return;
            }

            node = nodeList[lastFindCount];
            parentFrm.tvDocType.SelectedNode = node;        //选中查找到的节点
            parentFrm.tvDocType.Focus();
        }

 

阅读更多
个人分类: C#
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭