首页 > 开发 > 综合 > 正文

treeview的源代码

2024-07-21 02:16:49
字体:
来源:转载
供稿:网友
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)[email protected]"/")
        [email protected]"/";
      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());
  }
}(转自:http://www.c-sharpcorner.com/winforms/usingtreeviewpsd.asp)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表