常用控件介绍 — TreeView和ListView(打造资源管理器)

 

  1 None.gif ' 常用控件介绍 — TreeView和ListView
  2 None.gif
  3 None.gif Imports  System.IO
  4 ExpandedBlockStart.gifContractedBlock.gif Public   Class Form1 Class Form1
  5InBlock.gif    Inherits System.Windows.Forms.Form
  6InBlock.gif
  7ContractedSubBlock.gifExpandedSubBlockStart.gifWindows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
  8InBlock.gif
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    Public Sub New()Sub New()
 10InBlock.gif        MyBase.New()
 11InBlock.gif
 12InBlock.gif        '该调用是 Windows 窗体设计器所必需的。
 13InBlock.gif        InitializeComponent()
 14InBlock.gif
 15InBlock.gif        '在 InitializeComponent() 调用之后添加任何初始化
 16InBlock.gif
 17ExpandedSubBlockEnd.gif    End Sub

 18InBlock.gif
 19InBlock.gif    '窗体重写 dispose 以清理组件列表。
 20ExpandedSubBlockStart.gifContractedSubBlock.gif    Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
 21InBlock.gif        If disposing Then
 22InBlock.gif            If Not (components Is NothingThen
 23InBlock.gif                components.Dispose()
 24InBlock.gif            End If
 25InBlock.gif        End If
 26InBlock.gif        MyBase.Dispose(disposing)
 27ExpandedSubBlockEnd.gif    End Sub

 28InBlock.gif
 29InBlock.gif    'Windows 窗体设计器所必需的
 30InBlock.gif    Private components As System.ComponentModel.IContainer
 31InBlock.gif
 32InBlock.gif    '注意: 以下过程是 Windows 窗体设计器所必需的
 33InBlock.gif    '可以使用 Windows 窗体设计器修改此过程。
 34InBlock.gif    '不要使用代码编辑器修改它。
 35InBlock.gif    Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
 36InBlock.gif    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
 37InBlock.gif    Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
 38InBlock.gif    Friend WithEvents ListView1 As System.Windows.Forms.ListView
 39InBlock.gif    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
 40InBlock.gif    Friend WithEvents FileName As System.Windows.Forms.ColumnHeader
 41InBlock.gif    Friend WithEvents LastAccess As System.Windows.Forms.ColumnHeader
 42ExpandedSubBlockStart.gifContractedSubBlock.gif    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
 43InBlock.gif        Me.components = New System.ComponentModel.Container
 44InBlock.gif        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
 45InBlock.gif        Me.StatusBar1 = New System.Windows.Forms.StatusBar
 46InBlock.gif        Me.TreeView1 = New System.Windows.Forms.TreeView
 47InBlock.gif        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
 48InBlock.gif        Me.Splitter1 = New System.Windows.Forms.Splitter
 49InBlock.gif        Me.ListView1 = New System.Windows.Forms.ListView
 50InBlock.gif        Me.FileName = New System.Windows.Forms.ColumnHeader
 51InBlock.gif        Me.LastAccess = New System.Windows.Forms.ColumnHeader
 52InBlock.gif        Me.SuspendLayout()
 53InBlock.gif        '
 54InBlock.gif        'StatusBar1
 55InBlock.gif        '
 56InBlock.gif        Me.StatusBar1.Location = New System.Drawing.Point(0328)
 57InBlock.gif        Me.StatusBar1.Name = "StatusBar1"
 58InBlock.gif        Me.StatusBar1.Size = New System.Drawing.Size(54422)
 59InBlock.gif        Me.StatusBar1.TabIndex = 0
 60InBlock.gif        Me.StatusBar1.Text = "StatusBar1"
 61InBlock.gif        '
 62InBlock.gif        'TreeView1
 63InBlock.gif        '
 64InBlock.gif        Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Left
 65InBlock.gif        Me.TreeView1.ImageList = Me.ImageList1
 66InBlock.gif        Me.TreeView1.Location = New System.Drawing.Point(00)
 67InBlock.gif        Me.TreeView1.Name = "TreeView1"
 68InBlock.gif        Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("我的电脑"00)})
 69InBlock.gif        Me.TreeView1.Size = New System.Drawing.Size(121328)
 70InBlock.gif        Me.TreeView1.TabIndex = 1
 71InBlock.gif        '
 72InBlock.gif        'ImageList1
 73InBlock.gif        '
 74InBlock.gif        Me.ImageList1.ImageSize = New System.Drawing.Size(1616)
 75InBlock.gif        Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
 76InBlock.gif        Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
 77InBlock.gif        '
 78InBlock.gif        'Splitter1
 79InBlock.gif        '
 80InBlock.gif        Me.Splitter1.Location = New System.Drawing.Point(1210)
 81InBlock.gif        Me.Splitter1.Name = "Splitter1"
 82InBlock.gif        Me.Splitter1.Size = New System.Drawing.Size(3328)
 83InBlock.gif        Me.Splitter1.TabIndex = 2
 84InBlock.gif        Me.Splitter1.TabStop = False
 85InBlock.gif        '
 86InBlock.gif        'ListView1
 87InBlock.gif        '
 88InBlock.gif        Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.FileName, Me.LastAccess})
 89InBlock.gif        Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
 90InBlock.gif        Me.ListView1.Location = New System.Drawing.Point(1240)
 91InBlock.gif        Me.ListView1.Name = "ListView1"
 92InBlock.gif        Me.ListView1.Size = New System.Drawing.Size(420328)
 93InBlock.gif        Me.ListView1.TabIndex = 3
 94InBlock.gif        Me.ListView1.View = System.Windows.Forms.View.Details
 95InBlock.gif        '
 96InBlock.gif        'FileName
 97InBlock.gif        '
 98InBlock.gif        Me.FileName.Text = "文件名称"
 99InBlock.gif        Me.FileName.Width = 200
100InBlock.gif        '
101InBlock.gif        'LastAccess
102InBlock.gif        '
103InBlock.gif        Me.LastAccess.Text = "最后访问时间"
104InBlock.gif        Me.LastAccess.Width = 200
105InBlock.gif        '
106InBlock.gif        'Form1
107InBlock.gif        '
108InBlock.gif        Me.AutoScaleBaseSize = New System.Drawing.Size(614)
109InBlock.gif        Me.ClientSize = New System.Drawing.Size(544350)
110InBlock.gif        Me.Controls.Add(Me.ListView1)
111InBlock.gif        Me.Controls.Add(Me.Splitter1)
112InBlock.gif        Me.Controls.Add(Me.TreeView1)
113InBlock.gif        Me.Controls.Add(Me.StatusBar1)
114InBlock.gif        Me.Name = "Form1"
115InBlock.gif        Me.Text = "Form1"
116InBlock.gif        Me.ResumeLayout(False)
117InBlock.gif
118ExpandedSubBlockEnd.gif    End Sub

119InBlock.gif
120ExpandedSubBlockEnd.gif#End Region

121InBlock.gif
122ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub TreeView1_AfterSelect()Sub TreeView1_AfterSelect(ByVal sender As ObjectByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
123InBlock.gif        If e.Node.Text = "我的电脑" Then
124InBlock.gif            '列举驱动器
125InBlock.gif            EnumDriver(e.Node)
126InBlock.gif        Else
127InBlock.gif            '列举子文件夹。
128InBlock.gif            EnumDir(e.Node)
129InBlock.gif        End If
130ExpandedSubBlockEnd.gif    End Sub

131InBlock.gif
132InBlock.gif    '列举驱动器
133ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub EnumDriver()Sub EnumDriver(ByVal node As TreeNode)
134InBlock.gif        Dim drv As String
135InBlock.gif        If node.Nodes.Count = 0 Then
136InBlock.gif            For Each drv In Directory.GetLogicalDrives
137InBlock.gif                TreeView1.SelectedNode = node
138InBlock.gif                Dim tmpNode As New TreeNode
139InBlock.gif                tmpNode.Text = drv.Substring(0, drv.Length - 1)
140InBlock.gif                tmpNode.Tag = drv
141InBlock.gif                tmpNode.ImageIndex = 1
142InBlock.gif                tmpNode.SelectedImageIndex = 1
143InBlock.gif
144InBlock.gif                TreeView1.SelectedNode.Nodes.Add(tmpNode)
145InBlock.gif                TreeView1.SelectedNode.Nodes(TreeView1.SelectedNode.Nodes.Count - 1).EnsureVisible()
146InBlock.gif            Next
147InBlock.gif        End If
148InBlock.gif
149ExpandedSubBlockEnd.gif    End Sub

150InBlock.gif
151InBlock.gif    '列举子文件夹名称
152ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub EnumDir()Sub EnumDir(ByVal node As TreeNode)
153InBlock.gif        TreeView1.SelectedNode = node
154InBlock.gif        Dim DirectoryPath As String = node.Tag.ToString()
155InBlock.gif        If node.Nodes.Count = 0 Then
156InBlock.gif            If DirectoryPath.Substring(DirectoryPath.Length - 1<> "\" Then
157InBlock.gif                DirectoryPath += "\"
158InBlock.gif            End If
159InBlock.gif
160InBlock.gif            Try
161InBlock.gif                Dim Dir As String
162InBlock.gif                For Each Dir In Directory.GetDirectories(DirectoryPath)
163InBlock.gif                    Dim tmpNode As New TreeNode
164InBlock.gif                    tmpNode.Text = Dir.Substring(Dir.LastIndexOf("\"+ 1)
165InBlock.gif                    tmpNode.Tag() = Dir
166InBlock.gif                    tmpNode.ImageIndex = 2
167InBlock.gif                    tmpNode.SelectedImageIndex = 3
168InBlock.gif                    TreeView1.SelectedNode.Nodes.Add(tmpNode)
169InBlock.gif                    TreeView1.SelectedNode.Nodes(TreeView1.SelectedNode.Nodes.Count - 1).EnsureVisible()
170InBlock.gif                Next
171InBlock.gif            Catch ex As Exception
172InBlock.gif            End Try
173InBlock.gif        End If
174InBlock.gif        EnumFile(node)  '列举完文件夹之后再列表显示这个文件夹中的文件。
175ExpandedSubBlockEnd.gif    End Sub

176InBlock.gif
177InBlock.gif    '列举文件夹下面的文件名称
178ExpandedSubBlockStart.gifContractedSubBlock.gif    Private Sub EnumFile()Sub EnumFile(ByVal node As TreeNode)
179InBlock.gif        Dim DirectoryPath As String = node.Tag.ToString()
180InBlock.gif        If DirectoryPath.Substring(DirectoryPath.Length - 1<> "\" Then
181InBlock.gif            DirectoryPath += "\"
182InBlock.gif        End If
183InBlock.gif        Try
184InBlock.gif            ListView1.Items.Clear()
185InBlock.gif            Dim tmpFile As String
186InBlock.gif            Dim lvItem As ListViewItem
187InBlock.gif            For Each tmpFile In Directory.GetFiles(DirectoryPath)
188InBlock.gif                lvItem = New ListViewItem(Path.GetFileName(tmpFile))   '文件名称
189InBlock.gif                lvItem.SubItems.Add(File.GetLastAccessTime(tmpFile))   '最后访问时间
190InBlock.gif                ListView1.Items.Add(lvItem)                            '将这个项目加载到ListView中
191InBlock.gif            Next
192InBlock.gif        Catch ex As Exception
193InBlock.gif        End Try
194ExpandedSubBlockEnd.gif    End Sub

195InBlock.gif
196ExpandedBlockEnd.gifEnd Class

197 None.gif

转载于:https://www.cnblogs.com/sxg1010/archive/2006/07/28/462420.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值