怎样才能实现treeview和另一个容器控件的连接,实现window文件管理器的效果
怎么把treeview和listview连接起来啊
在“文件”菜单上指向“新建”,然后单击“项目”。
在“新建项目”对话框中,请执行以下操作:
在“项目类型”窗格中选择“Visual Basic 项目”或“Visual C# 项目”。
在“模板”窗格中选择“Windows 应用程序”。
单击“确定”。新建了一个 Windows 窗体项目。
向该窗体添加一个 SplitContainer 控件,并将其 Dock 属性设置为 Fill。
向该窗体添加一个名为 imageList1 的 ImageList,然后使用属性浏览器按所列顺序添加两个图标:一个文件夹图标和一个文档图标。
向该窗体添加一个名为 treeview1 的 TreeView 控件,将其置于 SplitContainer 控件的左侧。在 treeView1 的属性浏览器中执行以下操作:
将 Dock 属性设置为 Fill。
将 ImageList 属性设置为 imagelist1.
向该窗体中添加一个名为 listView1 的 ListView 控件,将其置于 SplitContainer 控件的右侧。在 listview1 的属性浏览器中执行以下操作:
将 Dock 属性设置为 Fill。
将 View 属性设置为 Details。
单击 Columns 属性中的椭圆 (),以打开 ColumnHeader 集合编辑器。添加三列,并将其 Text 属性分别设置为 Name、Type 和 Last Modified。单击“确定”关闭对话框。
将 SmallImageList 属性设置为 imageList1.
实现代码以便将节点和子节点填充到 TreeView 中。示例代码从文件系统读取,并且要求应用程序目录中存在 folder.ico 和 doc.ico, 这两个图标。
Visual Basic 复制代码
Private Sub PopulateTreeView()
Dim rootNode As TreeNode
Dim info As New DirectoryInfo("C:\Documents and Settings")
If info.Exists Then
rootNode = New TreeNode(info.Name)
rootNode.Tag = info
GetDirectories(info.GetDirectories(), rootNode)
treeView1.Nodes.Add(rootNode)
End If
End Sub
Private Sub GetDirectories(ByVal subDirs() As DirectoryInfo, _
ByVal nodeToAddTo As TreeNode)
Dim aNode As TreeNode
Dim subSubDirs() As DirectoryInfo
Dim subDir As DirectoryInfo
For Each subDir In subDirs
aNode = New TreeNode(subDir.Name, 0, 0)
aNode.Tag = subDir
aNode.ImageKey = "folder"
subSubDirs = subDir.GetDirectories()
If subSubDirs.Length <> 0 Then
GetDirectories(subSubDirs, aNode)
End If
nodeToAddTo.Nodes.Add(aNode)
Next subDir
End Sub
C# 复制代码
private void PopulateTreeView()
{
TreeNode rootNode;
DirectoryInfo info = new DirectoryInfo(@"C:\Documents and Settings");
if (info.Exists)
{
rootNode = new TreeNode(info.Name);
rootNode.Tag = info;
GetDirectories(info.GetDirectories(), rootNode);
treeView1.Nodes.Add(rootNode);
}
}
private void GetDirectories(DirectoryInfo[] subDirs,
TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir;
aNode.ImageKey = "folder";
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
在窗体的构造函数或 Load 事件处理方法中,调用上一步中的设置方法。
Visual Basic 复制代码
Public Sub New()
InitializeComponent()
PopulateTreeView()
End Sub 'New
C# 复制代码
public Form1()
{
InitializeComponent();
PopulateTreeView();
}
处理 treeview1 的 NodeMouseClick 事件,实现代码以便在单击某个节点时,用该节点的内容来填充 listview1。
Visual Basic 复制代码
Private Sub treeView1_NodeMouseClick(ByVal sender As Object, _
ByVal e As TreeNodeMouseClickEventArgs) _
Handles treeView1.NodeMouseClick
Dim newSelected As TreeNode = e.Node
listView1.Items.Clear()
Dim nodeDirInfo As DirectoryInfo = _
CType(newSelected.Tag, DirectoryInfo)
Dim subItems() As ListViewItem.ListViewSubItem
Dim item As ListViewItem = Nothing
Dim dir As DirectoryInfo
For Each dir In nodeDirInfo.GetDirectories()
item = New ListViewItem(dir.Name, 0)
subItems = New ListViewItem.ListViewSubItem() _
{New ListViewItem.ListViewSubItem(item, "Directory"), _
New ListViewItem.ListViewSubItem(item, _
dir.LastAccessTime.ToShortDateString())}
item.SubItems.AddRange(subItems)
listView1.Items.Add(item)
Next dir
Dim file As FileInfo
For Each file In nodeDirInfo.GetFiles()
item = New ListViewItem(file.Name, 1)
subItems = New ListViewItem.ListViewSubItem() _
{New ListViewItem.ListViewSubItem(item, "File"), _
New ListViewItem.ListViewSubItem(item, _
file.LastAccessTime.ToShortDateString())}
item.SubItems.AddRange(subItems)
listView1.Items.Add(item)
Next file
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
End Sub
C# 复制代码
void treeView1_NodeMouseClick(object sender,
TreeNodeMouseClickEventArgs e)
{
TreeNode newSelected = e.Node;
listView1.Items.Clear();
DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
ListViewItem.ListViewSubItem[] subItems;
ListViewItem item = null;
foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
{
item = new ListViewItem(dir.Name, 0);
subItems = new ListViewItem.ListViewSubItem[]
{new ListViewItem.ListViewSubItem(item, "Directory"),
new ListViewItem.ListViewSubItem(item,
dir.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
foreach (FileInfo file in nodeDirInfo.GetFiles())
{
item = new ListViewItem(file.Name, 1);
subItems = new ListViewItem.ListViewSubItem[]
{ new ListViewItem.ListViewSubItem(item, "File"),
new ListViewItem.ListViewSubItem(item,
file.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
C# 复制代码
void treeView1_NodeMouseClick(object sender,
TreeNodeMouseClickEventArgs e)
{
TreeNode newSelected = e.Node;
listView1.Items.Clear();
DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
ListViewItem.ListViewSubItem[] subItems;
ListViewItem item = null;
foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
{
item = new ListViewItem(dir.Name, 0);
subItems = new ListViewItem.ListViewSubItem[]
{new ListViewItem.ListViewSubItem(item, "Directory"),
new ListViewItem.ListViewSubItem(item,
dir.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
foreach (FileInfo file in nodeDirInfo.GetFiles())
{
item = new ListViewItem(file.Name, 1);
subItems = new ListViewItem.ListViewSubItem[]
{ new ListViewItem.ListViewSubItem(item, "File"),
new ListViewItem.ListViewSubItem(item,
file.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
我将这些添加后运行,出现错误说是不是缺少using命令
这是怎么会事啊