1'常用控件介绍 — TreeView和ListView
2
3Imports System.IO
4Public Class Form1Class Form1
5 Inherits System.Windows.Forms.Form
6
7Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
8
9 Public Sub New()Sub New()
10 MyBase.New()
11
12 '该调用是 Windows 窗体设计器所必需的。
13 InitializeComponent()
14
15 '在 InitializeComponent() 调用之后添加任何初始化
16
17 End Sub
18
19 '窗体重写 dispose 以清理组件列表。
20 Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
21 If disposing Then
22 If Not (components Is Nothing) Then
23 components.Dispose()
24 End If
25 End If
26 MyBase.Dispose(disposing)
27 End Sub
28
29 'Windows 窗体设计器所必需的
30 Private components As System.ComponentModel.IContainer
31
32 '注意: 以下过程是 Windows 窗体设计器所必需的
33 '可以使用 Windows 窗体设计器修改此过程。
34 '不要使用代码编辑器修改它。
35 Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
36 Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
37 Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
38 Friend WithEvents ListView1 As System.Windows.Forms.ListView
39 Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
40 Friend WithEvents FileName As System.Windows.Forms.ColumnHeader
41 Friend WithEvents LastAccess As System.Windows.Forms.ColumnHeader
42 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
43 Me.components = New System.ComponentModel.Container
44 Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
45 Me.StatusBar1 = New System.Windows.Forms.StatusBar
46 Me.TreeView1 = New System.Windows.Forms.TreeView
47 Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
48 Me.Splitter1 = New System.Windows.Forms.Splitter
49 Me.ListView1 = New System.Windows.Forms.ListView
50 Me.FileName = New System.Windows.Forms.ColumnHeader
51 Me.LastAccess = New System.Windows.Forms.ColumnHeader
52 Me.SuspendLayout()
53 '
54 'StatusBar1
55 '
56 Me.StatusBar1.Location = New System.Drawing.Point(0, 328)
57 Me.StatusBar1.Name = "StatusBar1"
58 Me.StatusBar1.Size = New System.Drawing.Size(544, 22)
59 Me.StatusBar1.TabIndex = 0
60 Me.StatusBar1.Text = "StatusBar1"
61 '
62 'TreeView1
63 '
64 Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Left
65 Me.TreeView1.ImageList = Me.ImageList1
66 Me.TreeView1.Location = New System.Drawing.Point(0, 0)
67 Me.TreeView1.Name = "TreeView1"
68 Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("我的电脑", 0, 0)})
69 Me.TreeView1.Size = New System.Drawing.Size(121, 328)
70 Me.TreeView1.TabIndex = 1
71 '
72 'ImageList1
73 '
74 Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
75 Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
76 Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
77 '
78 'Splitter1
79 '
80 Me.Splitter1.Location = New System.Drawing.Point(121, 0)
81 Me.Splitter1.Name = "Splitter1"
82 Me.Splitter1.Size = New System.Drawing.Size(3, 328)
83 Me.Splitter1.TabIndex = 2
84 Me.Splitter1.TabStop = False
85 '
86 'ListView1
87 '
88 Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.FileName, Me.LastAccess})
89 Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
90 Me.ListView1.Location = New System.Drawing.Point(124, 0)
91 Me.ListView1.Name = "ListView1"
92 Me.ListView1.Size = New System.Drawing.Size(420, 328)
93 Me.ListView1.TabIndex = 3
94 Me.ListView1.View = System.Windows.Forms.View.Details
95 '
96 'FileName
97 '
98 Me.FileName.Text = "文件名称"
99 Me.FileName.Width = 200
100 '
101 'LastAccess
102 '
103 Me.LastAccess.Text = "最后访问时间"
104 Me.LastAccess.Width = 200
105 '
106 'Form1
107 '
108 Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
109 Me.ClientSize = New System.Drawing.Size(544, 350)
110 Me.Controls.Add(Me.ListView1)
111 Me.Controls.Add(Me.Splitter1)
112 Me.Controls.Add(Me.TreeView1)
113 Me.Controls.Add(Me.StatusBar1)
114 Me.Name = "Form1"
115 Me.Text = "Form1"
116 Me.ResumeLayout(False)
117
118 End Sub
119
120#End Region
121
122 Private Sub TreeView1_AfterSelect()Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
123 If e.Node.Text = "我的电脑" Then
124 '列举驱动器
125 EnumDriver(e.Node)
126 Else
127 '列举子文件夹。
128 EnumDir(e.Node)
129 End If
130 End Sub
131
132 '列举驱动器
133 Private Sub EnumDriver()Sub EnumDriver(ByVal node As TreeNode)
134 Dim drv As String
135 If node.Nodes.Count = 0 Then
136 For Each drv In Directory.GetLogicalDrives
137 TreeView1.SelectedNode = node
138 Dim tmpNode As New TreeNode
139 tmpNode.Text = drv.Substring(0, drv.Length - 1)
140 tmpNode.Tag = drv
141 tmpNode.ImageIndex = 1
142 tmpNode.SelectedImageIndex = 1
143
144 TreeView1.SelectedNode.Nodes.Add(tmpNode)
145 TreeView1.SelectedNode.Nodes(TreeView1.SelectedNode.Nodes.Count - 1).EnsureVisible()
146 Next
147 End If
148
149 End Sub
150
151 '列举子文件夹名称
152 Private Sub EnumDir()Sub EnumDir(ByVal node As TreeNode)
153 TreeView1.SelectedNode = node
154 Dim DirectoryPath As String = node.Tag.ToString()
155 If node.Nodes.Count = 0 Then
156 If DirectoryPath.Substring(DirectoryPath.Length - 1) <> "\" Then
157 DirectoryPath += "\"
158 End If
159
160 Try
161 Dim Dir As String
162 For Each Dir In Directory.GetDirectories(DirectoryPath)
163 Dim tmpNode As New TreeNode
164 tmpNode.Text = Dir.Substring(Dir.LastIndexOf("\") + 1)
165 tmpNode.Tag() = Dir
166 tmpNode.ImageIndex = 2
167 tmpNode.SelectedImageIndex = 3
168 TreeView1.SelectedNode.Nodes.Add(tmpNode)
169 TreeView1.SelectedNode.Nodes(TreeView1.SelectedNode.Nodes.Count - 1).EnsureVisible()
170 Next
171 Catch ex As Exception
172 End Try
173 End If
174 EnumFile(node) '列举完文件夹之后再列表显示这个文件夹中的文件。
175 End Sub
176
177 '列举文件夹下面的文件名称
178 Private Sub EnumFile()Sub EnumFile(ByVal node As TreeNode)
179 Dim DirectoryPath As String = node.Tag.ToString()
180 If DirectoryPath.Substring(DirectoryPath.Length - 1) <> "\" Then
181 DirectoryPath += "\"
182 End If
183 Try
184 ListView1.Items.Clear()
185 Dim tmpFile As String
186 Dim lvItem As ListViewItem
187 For Each tmpFile In Directory.GetFiles(DirectoryPath)
188 lvItem = New ListViewItem(Path.GetFileName(tmpFile)) '文件名称
189 lvItem.SubItems.Add(File.GetLastAccessTime(tmpFile)) '最后访问时间
190 ListView1.Items.Add(lvItem) '将这个项目加载到ListView中
191 Next
192 Catch ex As Exception
193 End Try
194 End Sub
195
196End Class
197
2
3Imports System.IO
4Public Class Form1Class Form1
5 Inherits System.Windows.Forms.Form
6
7Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
8
9 Public Sub New()Sub New()
10 MyBase.New()
11
12 '该调用是 Windows 窗体设计器所必需的。
13 InitializeComponent()
14
15 '在 InitializeComponent() 调用之后添加任何初始化
16
17 End Sub
18
19 '窗体重写 dispose 以清理组件列表。
20 Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
21 If disposing Then
22 If Not (components Is Nothing) Then
23 components.Dispose()
24 End If
25 End If
26 MyBase.Dispose(disposing)
27 End Sub
28
29 'Windows 窗体设计器所必需的
30 Private components As System.ComponentModel.IContainer
31
32 '注意: 以下过程是 Windows 窗体设计器所必需的
33 '可以使用 Windows 窗体设计器修改此过程。
34 '不要使用代码编辑器修改它。
35 Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
36 Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
37 Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
38 Friend WithEvents ListView1 As System.Windows.Forms.ListView
39 Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
40 Friend WithEvents FileName As System.Windows.Forms.ColumnHeader
41 Friend WithEvents LastAccess As System.Windows.Forms.ColumnHeader
42 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
43 Me.components = New System.ComponentModel.Container
44 Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
45 Me.StatusBar1 = New System.Windows.Forms.StatusBar
46 Me.TreeView1 = New System.Windows.Forms.TreeView
47 Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
48 Me.Splitter1 = New System.Windows.Forms.Splitter
49 Me.ListView1 = New System.Windows.Forms.ListView
50 Me.FileName = New System.Windows.Forms.ColumnHeader
51 Me.LastAccess = New System.Windows.Forms.ColumnHeader
52 Me.SuspendLayout()
53 '
54 'StatusBar1
55 '
56 Me.StatusBar1.Location = New System.Drawing.Point(0, 328)
57 Me.StatusBar1.Name = "StatusBar1"
58 Me.StatusBar1.Size = New System.Drawing.Size(544, 22)
59 Me.StatusBar1.TabIndex = 0
60 Me.StatusBar1.Text = "StatusBar1"
61 '
62 'TreeView1
63 '
64 Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Left
65 Me.TreeView1.ImageList = Me.ImageList1
66 Me.TreeView1.Location = New System.Drawing.Point(0, 0)
67 Me.TreeView1.Name = "TreeView1"
68 Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("我的电脑", 0, 0)})
69 Me.TreeView1.Size = New System.Drawing.Size(121, 328)
70 Me.TreeView1.TabIndex = 1
71 '
72 'ImageList1
73 '
74 Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
75 Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
76 Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
77 '
78 'Splitter1
79 '
80 Me.Splitter1.Location = New System.Drawing.Point(121, 0)
81 Me.Splitter1.Name = "Splitter1"
82 Me.Splitter1.Size = New System.Drawing.Size(3, 328)
83 Me.Splitter1.TabIndex = 2
84 Me.Splitter1.TabStop = False
85 '
86 'ListView1
87 '
88 Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.FileName, Me.LastAccess})
89 Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
90 Me.ListView1.Location = New System.Drawing.Point(124, 0)
91 Me.ListView1.Name = "ListView1"
92 Me.ListView1.Size = New System.Drawing.Size(420, 328)
93 Me.ListView1.TabIndex = 3
94 Me.ListView1.View = System.Windows.Forms.View.Details
95 '
96 'FileName
97 '
98 Me.FileName.Text = "文件名称"
99 Me.FileName.Width = 200
100 '
101 'LastAccess
102 '
103 Me.LastAccess.Text = "最后访问时间"
104 Me.LastAccess.Width = 200
105 '
106 'Form1
107 '
108 Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
109 Me.ClientSize = New System.Drawing.Size(544, 350)
110 Me.Controls.Add(Me.ListView1)
111 Me.Controls.Add(Me.Splitter1)
112 Me.Controls.Add(Me.TreeView1)
113 Me.Controls.Add(Me.StatusBar1)
114 Me.Name = "Form1"
115 Me.Text = "Form1"
116 Me.ResumeLayout(False)
117
118 End Sub
119
120#End Region
121
122 Private Sub TreeView1_AfterSelect()Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
123 If e.Node.Text = "我的电脑" Then
124 '列举驱动器
125 EnumDriver(e.Node)
126 Else
127 '列举子文件夹。
128 EnumDir(e.Node)
129 End If
130 End Sub
131
132 '列举驱动器
133 Private Sub EnumDriver()Sub EnumDriver(ByVal node As TreeNode)
134 Dim drv As String
135 If node.Nodes.Count = 0 Then
136 For Each drv In Directory.GetLogicalDrives
137 TreeView1.SelectedNode = node
138 Dim tmpNode As New TreeNode
139 tmpNode.Text = drv.Substring(0, drv.Length - 1)
140 tmpNode.Tag = drv
141 tmpNode.ImageIndex = 1
142 tmpNode.SelectedImageIndex = 1
143
144 TreeView1.SelectedNode.Nodes.Add(tmpNode)
145 TreeView1.SelectedNode.Nodes(TreeView1.SelectedNode.Nodes.Count - 1).EnsureVisible()
146 Next
147 End If
148
149 End Sub
150
151 '列举子文件夹名称
152 Private Sub EnumDir()Sub EnumDir(ByVal node As TreeNode)
153 TreeView1.SelectedNode = node
154 Dim DirectoryPath As String = node.Tag.ToString()
155 If node.Nodes.Count = 0 Then
156 If DirectoryPath.Substring(DirectoryPath.Length - 1) <> "\" Then
157 DirectoryPath += "\"
158 End If
159
160 Try
161 Dim Dir As String
162 For Each Dir In Directory.GetDirectories(DirectoryPath)
163 Dim tmpNode As New TreeNode
164 tmpNode.Text = Dir.Substring(Dir.LastIndexOf("\") + 1)
165 tmpNode.Tag() = Dir
166 tmpNode.ImageIndex = 2
167 tmpNode.SelectedImageIndex = 3
168 TreeView1.SelectedNode.Nodes.Add(tmpNode)
169 TreeView1.SelectedNode.Nodes(TreeView1.SelectedNode.Nodes.Count - 1).EnsureVisible()
170 Next
171 Catch ex As Exception
172 End Try
173 End If
174 EnumFile(node) '列举完文件夹之后再列表显示这个文件夹中的文件。
175 End Sub
176
177 '列举文件夹下面的文件名称
178 Private Sub EnumFile()Sub EnumFile(ByVal node As TreeNode)
179 Dim DirectoryPath As String = node.Tag.ToString()
180 If DirectoryPath.Substring(DirectoryPath.Length - 1) <> "\" Then
181 DirectoryPath += "\"
182 End If
183 Try
184 ListView1.Items.Clear()
185 Dim tmpFile As String
186 Dim lvItem As ListViewItem
187 For Each tmpFile In Directory.GetFiles(DirectoryPath)
188 lvItem = New ListViewItem(Path.GetFileName(tmpFile)) '文件名称
189 lvItem.SubItems.Add(File.GetLastAccessTime(tmpFile)) '最后访问时间
190 ListView1.Items.Add(lvItem) '将这个项目加载到ListView中
191 Next
192 Catch ex As Exception
193 End Try
194 End Sub
195
196End Class
197
来源:https://www.cnblogs.com/sxg1010/archive/2006/07/28/462420.html