C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

在Form中构建给定目录的Treeview

using  System;
using  System.IO;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Data;

namespace  WindowsApplication
{
    
///   <summary>
    
///  Summary description for Form1.
    
///   </summary>

     public   class  Form1 : System.Windows.Forms.Form
    
{
        
private  System.Windows.Forms.TreeView treeView1;
        
///   <summary>
        
///  Required designer variable.
        
///   </summary>

         private  System.ComponentModel.Container components  =   null ;

        
public  Form1()
        
{
            
//
            
//  Required for Windows Form Designer support
            
//
            InitializeComponent();

            
//
            
//  TODO: Add any constructor code after InitializeComponent call
            
//
            Text  = " my explore " ;
        }


        
///   <summary>
        
///  Clean up any resources being used.
        
///   </summary>

         protected   override   void  Dispose(  bool  disposing )
        
{
            
if ( disposing )
            
{
                
if  (components  !=   null
                
{
                    components.Dispose();
                }

            }

            
base .Dispose( disposing );
        }


        
Windows Form Designer generated code

        
private   void  Fill(TreeNode dirNode) 
        
{

            DirectoryInfo dir 
=   new  DirectoryInfo(dirNode.FullPath);
            
            
//  An exception could be thrown in this code if you don't
            
//  have sufficient security permissions for a file or directory.
            
//  You can catch and then ignore this exception.
             foreach  (DirectoryInfo dirItem  in  dir.GetDirectories()) 
            
{

                
//  Add node for the directory.
                TreeNode newNode  =   new  TreeNode(dirItem.Name);
                dirNode.Nodes.Add(newNode);
                newNode.Nodes.Add(
" * " );
            }

        }


        
///   <summary>
        
///  The main entry point for the application.
        
///   </summary>

        [STAThread]
        
static   void  Main() 
        
{
            Application.Run(
new  Form1());
        }


        
private   void  Form1_Load( object  sender, System.EventArgs e)
        
{
            
//  Set the first node.
            TreeNode rootNode  =   new  TreeNode( " C:\\ " );
            treeView1.Nodes.Add(rootNode);

            
//  Fill the first level and expand it.
            Fill(rootNode);
        treeView1.Nodes[
0 ].Expand();

        }


        
private   void  treeView1_BeforeExpand( object  sender, System.Windows.Forms.TreeViewCancelEventArgs e)
        
{
            
//  If a dummy node is found, remove it and read the 
            
//  real directory list.
             if  (e.Node.Nodes[ 0 ].Text  ==   " * "
            
{

                e.Node.Nodes.Clear();
                Fill(e.Node);
            }

        }

    }

}

posted on 2006-03-23 17:15 梦在天涯 阅读(499) 评论(0)  编辑 收藏 引用 所属分类: C#/.NET


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1784870
  • 排名 - 5

最新评论

阅读排行榜