在展开时选择C#treeview停止节点

时间:2011-09-05 09:25:33

标签: c# winforms treeview

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

    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);
            } 
        }
    }

1 个答案:

答案 0 :(得分:0)

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

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