c#树视图停止节点被选择展开
本文关键字:选择 节点 视图 | 更新日期: 2023-09-27 18:07:40
在我的应用程序中,我使用树视图。当用户单击[+]展开节点时,该节点将更改为所选节点。我怎样才能阻止这一切?
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);
}
}
}
正如Hans提到的,这不是标准WinForms TreeView的默认行为。但是,您可以在BeforeSelect事件中取消选择更改:
void tvFileStructure_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
if (iDontWantToSelectThis)
{
e.Cancel = true;
}
}