Microsoft . 技术之路...

—— 专注于微软技术, 分享是快乐的源泉......
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

TreeView的用法实例

Posted on 2006-01-11 22:09  赣江源  阅读(422)  评论(0编辑  收藏  举报
using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.IO;

class FolderDialog:Form
{
  TreeView FolderTree;
  ImageList FolderTreeIcons;
  TreeNode RootNode;
  string[] IconFileNames={"MyComp.ico","Drive.ico","OpenFold.ico","ClsdFold.ico"};
  public FolderDialog()
  {
    //Manipulate ImageList
    FolderTreeIcons=new ImageList();
    for(int i=0;i<IconFileNames.Length;i++)
    {
      FolderTreeIcons.Images.Add(Image.FromFile(@"Images\" + IconFileNames[i]));
    }
    
    //Setting Properties for TreeView
    FolderTree=new TreeView();
    FolderTree.AfterSelect+=new TreeViewEventHandler(Node_Select);
    FolderTree.ImageList=FolderTreeIcons;
    FolderTree.Anchor=AnchorStyles.Top|AnchorStyles.Bottom|AnchorStyles.Left|AnchorStyles.Right;
        

    //Root Node
    RootNode=new TreeNode();
    RootNode.Text="My Computer";
    RootNode.Tag="My Computer";
    RootNode.ImageIndex=0;
    FolderTree.Nodes.Add(RootNode);
    FolderTree.SelectedNode=RootNode;
    FolderTree.SelectedNode.ImageIndex=0;

    this.Text="Browse For Folder";
    this.Size=new Size(300,300);
    FolderTree.Size=new Size(290,275);
  
    this.Controls.Add(FolderTree);
  }

  private void EnumDrives(TreeNode ParentNode)
  {
    if(ParentNode.Nodes.Count==0)
    {    
      foreach(string drive in Directory.GetLogicalDrives())
      {
         FolderTree.SelectedNode=ParentNode;
         TreeNode TempNode=new TreeNode();
         TempNode.Text=drive.Substring(0,drive.Length-1);
         TempNode.Tag=drive;
         TempNode.ImageIndex=1;
         TempNode.SelectedImageIndex=1;
         FolderTree.SelectedNode.Nodes.Add(TempNode);
         FolderTree.SelectedNode.Nodes[FolderTree.SelectedNode.Nodes.Count-1].EnsureVisible();
      }
    }
  }

  private void EnumDirectories(TreeNode ParentNode)
  {
    FolderTree.SelectedNode=ParentNode;
    string DirectoryPath=ParentNode.Tag.ToString();
    if(ParentNode.Nodes.Count==0)
    {
      if(DirectoryPath.Substring(DirectoryPath.Length-1)!=@"\")
        DirectoryPath+=@"\";
      try
      {
        foreach(string directory in Directory.GetDirectories(DirectoryPath))
        {
          TreeNode TempNode=new TreeNode();
          TempNode.Text=directory.Substring(directory.LastIndexOf(@"\")+1);
          TempNode.Tag=directory;
          TempNode.ImageIndex=3;
          TempNode.SelectedImageIndex=2;
          FolderTree.SelectedNode.Nodes.Add(TempNode);
          FolderTree.SelectedNode.Nodes[FolderTree.SelectedNode.Nodes.Count-1].EnsureVisible();
        }
      }
      catch(Exception)
      {
      }
    }
}

  private void Node_Select(object sender,TreeViewEventArgs e)
  {
    if(e.Node.Tag.ToString()!="My Computer")
      EnumDirectories(e.Node);
    else
      EnumDrives(e.Node);
  }

  public static void Main()
  {
    Application.Run(new FolderDialog());
  }
}(