2011-09-05 45 views
1

在我的应用程序中,我使用了树视图。当用户单击[+]展开节点时,该节点将变为选定节点。我怎样才能阻止呢?在扩展中选择C#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); 
      } 
     } 
    } 
+2

WPF或WinForms的? – thumbmunkeys

+0

应用程序在winforms中 – bobble14988

+1

Winform的TreeView控件默认不会这样做。邮政编码,实际上再现了问题。 –

回答

0

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

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