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 btnFolder_Click(object sender, EventArgs e)
        {
            if (this.OwnedForms.Length > 0)
            {
                this.OwnedForms[0].Close();     //如果查询窗体已打开,则关闭已打开的窗口
            }
            TreeViewQuery frm = new TreeViewQuery(this);
            frm.ShowInTaskbar = false;
            frm.Show(this);
        }
 
查找窗体代码
 
       private FolderFrom parentFrm;
        private string lastFindString;  //记录上一次查找的文本
        private int lastFindCount;      //记录上一次查找的标记
        private List<TreeNode> nodeList = new List<TreeNode>();     //记录所有的树节点
        public TreeNode node = null;        //记录查找到的节点
        private bool IsUp = false;    //是否向上查找
        public TreeViewQuery ()
        {
            InitializeComponent();
        }
        public TreeViewQuery (FolderFrom 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.tvFolder.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.tvFolder.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.tvFolder.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.tvFolder.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 TreeViewQuery_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)        //判断是否找到节点
            {
               MessageBox.Show( "查找到尾部,未找到指定的节点!");
                this.Focus();
                lastFindCount = 0;
                lastFindString = "";
                return;
            }
            else if (lastFindCount == -2)        //判断是否找到节点
            {
                essageBox.Show("查找到顶部,未找到指定的节点!");
                this.Focus();
                lastFindCount = 0;
                lastFindString = "";
                return;
            }
            node = nodeList[lastFindCount];
            parentFrm.tvFolder.SelectedNode = node;        //选中查找到的节点
            parentFrm.tvFolder.Focus();
        }
 


你可能感兴趣的:(String,object,C#,null,events,methods)