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);
}
}
}
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;
}
}
精彩评论