C# 树视图停止在展开时选择节点

发布于 2024-12-03 03:09:31 字数 1593 浏览 0 评论 0原文

在我的应用程序中,我使用树视图。当用户单击 [+] 展开节点时,该节点将更改为所选节点。我怎样才能阻止这个?

    private void tvFileStructure_BeforeExpand(object sender, TreeViewCancelEventArgs e)
    {
        if (e.Node.Text != "Network")
        {
            int unauthorisedAccessExceptions = 0;

            try
            {
                TreeNode newSelected = e.Node;
                DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;

                TreeNode child = newSelected.FirstNode;
                if (child.Level < 3)
                {
                    while (child != null)
                    {
                        // Only try to populate if there aren't any children
                        if (child.FirstNode == null)
                        {
                            DirectoryInfo[] subDirs = ((DirectoryInfo)child.Tag).GetDirectories();
                            if (subDirs.Length != 0)
                            {
                                getDirectories(subDirs, child);
                            }
                        }

                        child = child.NextNode;
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                unauthorisedAccessExceptions++;
            }

            if (unauthorisedAccessExceptions > 0)
            {
                MessageBox.Show("There were " + unauthorisedAccessExceptions.ToString() + " folder(s) that you do not have access to.", "Warning...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            } 
        }
    }

In my application I am using a tree view. When the user clicks on the [+] to expand a node, that node changes to the selected node. How can I stop this?

    private void tvFileStructure_BeforeExpand(object sender, TreeViewCancelEventArgs e)
    {
        if (e.Node.Text != "Network")
        {
            int unauthorisedAccessExceptions = 0;

            try
            {
                TreeNode newSelected = e.Node;
                DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;

                TreeNode child = newSelected.FirstNode;
                if (child.Level < 3)
                {
                    while (child != null)
                    {
                        // Only try to populate if there aren't any children
                        if (child.FirstNode == null)
                        {
                            DirectoryInfo[] subDirs = ((DirectoryInfo)child.Tag).GetDirectories();
                            if (subDirs.Length != 0)
                            {
                                getDirectories(subDirs, child);
                            }
                        }

                        child = child.NextNode;
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                unauthorisedAccessExceptions++;
            }

            if (unauthorisedAccessExceptions > 0)
            {
                MessageBox.Show("There were " + unauthorisedAccessExceptions.ToString() + " folder(s) that you do not have access to.", "Warning...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            } 
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2024-12-10 03:09:31

正如 Hans 提到的,这不是标准 WinForms TreeView 的默认行为。但是,您可以在 BeforeSelect 事件中取消选择更改:

void tvFileStructure_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
    if (iDontWantToSelectThis)
    {
        e.Cancel = true;
    }
}

As Hans mentioned this is not the default behavior of the standard WinForms TreeView. However, you can cancel the selection changing in the BeforeSelect event:

void tvFileStructure_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
    if (iDontWantToSelectThis)
    {
        e.Cancel = true;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文