Пример #1
0
        /// <summary>
        /// Navigate the treeview to the path in the textbox. Must be called manually when CurrentPath changes.
        /// </summary>
        public void BrowseTo()
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                PopulateMyComputer();
                string textboxPath = txtPath.Text.ToLower();

                int      sentinel     = 1;
                TreeNode loopRootNode = nodeMyComputer;
                bool     network      = false;
                if (textboxPath.Contains(Desktop.ToLower()))
                {
                    loopRootNode = rootnodeDesktop;
                    if (textboxPath.Length == Desktop.Length)
                    {
                        loopRootNode.Expand();
                        loopRootNode.EnsureVisible();
                        return;
                    }
                }
                else if (textboxPath.Contains(MyDocuments.ToLower()))
                {
                    loopRootNode = nodeMyDocuments;
                    if (textboxPath.Length == MyDocuments.Length)
                    {
                        loopRootNode.Expand();
                        loopRootNode.EnsureVisible();
                        return;
                    }
                }
                else if (textboxPath == "my computer")
                {
                    loopRootNode.Expand();
                    loopRootNode.EnsureVisible();
                    return;
                }
                else if (textboxPath.StartsWith(@"\\"))
                {
                    loopRootNode = nodeEntireNetwork;
                    network      = true;
                }
                treeview1.SelectedNode = loopRootNode;
                if (!textboxPath.EndsWith(Seperator))
                {
                    textboxPath += Seperator;
                }

StartAgain:

                do
                {
                    foreach (TreeNode currNode in loopRootNode.Nodes)
                    {
                        string currNodePath = currNode.Tag.ToString().ToLower();
                        if (!currNodePath.EndsWith(Seperator))
                        {
                            currNodePath += Seperator;
                        }

                        if (textboxPath.StartsWith(currNodePath))
                        {
                            currNode.TreeView.Focus();
                            currNode.TreeView.SelectedNode = currNode;
                            //currNode.EnsureVisible();
                            NativeScroll.ScrollH(currNode, 37);
                            if (currNode.Nodes.Count > 0)
                            {
                                currNode.Expand();
                                loopRootNode = currNode;
                            }
                            else
                            {
                                if (textboxPath == currNodePath)
                                {
                                    sentinel = -1;
                                    break;
                                }
                                continue;
                            }
                            if (currNodePath.StartsWith(textboxPath))
                            {
                                sentinel = -1;
                                break;
                            }
                            goto StartAgain;
                        }
                        if (network)
                        {
                            loopRootNode = currNode;
                            goto StartAgain;
                        }
                    }
                    if (sentinel == -1)
                    {
                        break;
                    }
                    try
                    {
                        loopRootNode = loopRootNode.NextNode;
                    }
                    catch (Exception)
                    { } //no more nodes (not an error, so suppress)
                } while (sentinel >= 0);
            }
            catch (Exception e1)
            {
                MessageBox.Show("Error: " + e1.Message);
            }
            Cursor.Current = Cursors.Default;
        }