| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2713 人关注过本帖, 5 人收藏
标题:[分享]C#中TreeView组件使用方法初步
只看楼主 加入收藏
leely_lly
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-4-11
收藏
得分:0 

我现在也在使用这个控件,以下是我的代码,先粘到着再请教几个问题,万分感谢!

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


namespace Compress
{
public partial class FormFile : Form
{

public FormFile()
{
InitializeComponent();
}

private DirectoryInfo folder;


private void FormFile_Load(object sender, EventArgs e)
{
LoadTree();
}


private void LoadTree()
{
DirectoryInfo directory;

//clear the tree
//treeExplore.Nodes.Clear();


//Loop through the drive letter and find the available drive

foreach (string drive in Environment.GetLogicalDrives())
{
try
{
// get the directory information for this path
directory = new DirectoryInfo(drive);

//if the retrived directory is valid ,add it to the tree view
if (directory.Exists == true)
{
TreeNode newNode = new TreeNode(directory.FullName);
treeExplore.Nodes.Add(newNode);

//add the new node to the root level
GetSubDirectories(newNode);

}
}

catch
{
return;
}
}
}


private void GetSubDirectories(TreeNode parent)
{

DirectoryInfo directory;

try
{
//check if we have already scanned this folder

if (parent.Nodes.Count == 0)
{
directory = new DirectoryInfo(parent.FullPath);
foreach (DirectoryInfo dir in directory.GetDirectories())
{
TreeNode newNode = new TreeNode(dir.Name);
parent.Nodes.Add(newNode);

}
}

//Scan the frist level of sub folder to creat + or - sign
foreach (TreeNode node in parent.Nodes)
{

//if we have scanned this code before
if (node.Nodes.Count == 0)
{
//get the folder information for the special path
directory = new DirectoryInfo(node.FullPath);
foreach (DirectoryInfo dir in directory.GetDirectories())
{
//create a new node to add it to the tree

TreeNode newNode = new TreeNode(dir.Name);
node.Nodes.Add(newNode);
}
}
}
}

catch
{
return;
}
}

private void treeExplore_AfterSelect(object sender, TreeViewEventArgs e)
{
DirectoryInfo dirInfo = new DirectoryInfo(e.Node.FullPath);
listExplorer.Items.Clear();
if (dirInfo.Exists)
{
FileInfo[] fileInfo = dirInfo.GetFiles();
foreach (FileInfo info in fileInfo)
{
ListViewItem item = new ListViewItem();
item = listExplorer.Items.Add(info.Name);
item.SubItems.Add(info.LastAccessTime.ToString());
item.SubItems.Add(info.Length.ToString());
}
}
}

private void onMouseDown(object sender, MouseEventArgs e)
{
if (treeExplore.GetNodeAt(e.X, e.Y) != null)
{
compressMenuItem.Visible = true;
encryptMenuItem.Visible = true;
}
else
{
compressMenuItem.Visible=false;
encryptMenuItem.Visible=false;
}
}
本人也是才学习,对于以上代码也是基本按照例子做的,,加载本地机子上的文件系统,我现在主要是想在listExplorer又点击文件,有弹出菜单,若是.zip的就解压缩,若是别的就实现压缩的功能,怎么编写代码,还有就是要用哪个命名空间,使用什么类呢?跪求解答!

2007-04-11 15:45
天涯行客
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-3-17
收藏
得分:0 
好贴啊!学了不少好东西!感激不尽!
2007-04-19 23:36
wwzlgxwhx
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-8-15
收藏
得分:0 

谢谢楼主

2007-08-15 15:05
wang54554
Rank: 1
等 级:新手上路
帖 子:81
专家分:0
注 册:2007-10-27
收藏
得分:0 
顶下

2007-10-28 19:04
zh_hn
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2006-12-24
收藏
得分:0 
找的就是这个 顶

2007-12-01 15:43
快速回复:[分享]C#中TreeView组件使用方法初步
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025331 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved