大家很清楚TabControl元件的用法吧
不過接下來的程式將使用ListView元件及Panel元件設計類似的功能
先在表單上放2個元件,ListView元件以及Panel元件
引用:
作者: ListView1元件屬性
'
'ListView1
'
Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1})
Me.ListView1.Dock = System.Windows.Forms.DockStyle.Left
Me.ListView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None
Me.ListView1.HideSelection = False
Me.ListView1.LabelEdit = True
Me.ListView1.Location = New System.Drawing.Point(0, 0)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(185, 485)
Me.ListView1.StateImageList = Me.ImageList1
Me.ListView1.TabIndex = 4
Me.ListView1.UseCompatibleStateImageBehavior = False
Me.ListView1.View = System.Windows.Forms.View.Details
|
引用:
作者: ColumnHeader1元件屬性
'
'ColumnHeader1
'
Me.ColumnHeader1.Width = 120
|
引用:
作者: Panel1元件屬性
'
'Panel1
'
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.Panel1.Location = New System.Drawing.Point(185, 0)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(607, 485)
Me.Panel1.TabIndex = 5
|
引用:
作者: Form1.vb程式碼
Imports System.IO
Public Class Form1
Dim ListView(10) As Windows.Forms.ListView
Dim Column1(10) As ColumnHeader
Dim LVNO As Integer = 0
Dim ad As DriveInfo() = DriveInfo.GetDrives
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Single, j As Single = 0
For Each md As DriveInfo In ad
If LVNO >= 10 Then Exit For '我們只設10組陣列
If md.IsReady Then
ListView1.Items.Add(md.Name & md.VolumeLabel)
ListView1.Items(ListView1.Items.Count - 1).ToolTipText = md.Name
ListView1.Items(ListView1.Items.Count - 1).StateImageIndex = 0
ListView1.Items(ListView1.Items.Count - 1).Tag = LVNO
Panel1.Tag = LVNO
AddListView()
ScanFile(ListView1.Items(ListView1.Items.Count - 1).ToolTipText)
End If
Next
For i = 0 To LVNO - 1
ListView(i).Visible = False
Next
Panel1.Tag = 0
ListView(Panel1.Tag).Visible = True
End Sub
Sub AddListView()
'AddHandler dButton(i).Click, AddressOf dButton_Click
Dim i As Integer
For i = 0 To LVNO - 1
ListView(i).Visible = False
Next
Column1(LVNO) = New System.Windows.Forms.ColumnHeader
Column1(LVNO).Text = "名稱"
Column1(LVNO).Name = "Column1" & LVNO
Column1(LVNO).Width = 500
ListView(LVNO) = New System.Windows.Forms.ListView
ListView(LVNO).Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Column1(LVNO)})
ListView(LVNO).HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable
ListView(LVNO).Name = "ListView" & LVNO
ListView(LVNO).Dock = System.Windows.Forms.DockStyle.Fill
ListView(LVNO).View = System.Windows.Forms.View.Details
ListView(LVNO).FullRowSelect = True
ListView(LVNO).GridLines = True
ListView(LVNO).StateImageList = ImageList1
AddHandler ListView(LVNO).DoubleClick, AddressOf ListView_DoubleClick
Me.Panel1.Controls.Add(ListView(LVNO))
ListView(LVNO).Visible = True
LVNO += 1
End Sub
Private Sub ListView_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
If ListView(Panel1.Tag).Items(ListView(Panel1.Tag).FocusedItem.Index).StateImageIndex = 2 Then
ListView1.Items(ListView1.FocusedItem.Index).ToolTipText = My.Computer.FileSystem.GetParentPath(ListView1.Items(ListView1.FocusedItem.Index).ToolTipText)
ScanFile(ListView1.Items(ListView1.FocusedItem.Index).ToolTipText)
Else
ListView1.Items(ListView1.FocusedItem.Index).ToolTipText = My.Computer.FileSystem.CombinePath(ListView1.Items(ListView1.FocusedItem.Index).ToolTipText, ListView(Panel1.Tag).FocusedItem.Text)
ScanFile(ListView1.Items(ListView1.FocusedItem.Index).ToolTipText)
End If
End Sub
Private Sub ListView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.Click
Dim i As Integer
For i = 0 To ListView1.Items.Count - 1
ListView1.Items(i).StateImageIndex = 0
Next
ListView1.Items(ListView1.FocusedItem.Index).StateImageIndex = 1
For i = 0 To LVNO - 1
ListView(i).Visible = False
Next
ListView(ListView1.Items(ListView1.FocusedItem.Index).Tag).Visible = True
Panel1.Tag = ListView1.Items(ListView1.FocusedItem.Index).Tag
End Sub
Sub ScanFile(ByVal ScanPath As String)
Dim dir As New DirectoryInfo(ScanPath)
Dim dirs As DirectoryInfo() = dir.GetDirectories("*.*")
Dim diNext As DirectoryInfo
ListView(Panel1.Tag).Items.Clear()
For Each diNext In dirs
ListView(Panel1.Tag).Items.Add(diNext.ToString)
ListView(Panel1.Tag).Items(ListView(Panel1.Tag).Items.Count - 1).StateImageIndex = 0
Next
If ScanPath.Length > 3 Then
ListView(Panel1.Tag).Items.Add("..")
ListView(Panel1.Tag).Items(ListView(Panel1.Tag).Items.Count - 1).StateImageIndex = 2
End If
End Sub
Private Sub ListView1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.Resize
ListView1.Columns(0).Width = ListView1.Width - 22
End Sub
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
For i = 0 To LVNO - 1
ListView(i).Dispose()
Next
End Sub
End Class
|
建立ListView元件比建立表單的項目複雜了一些
因為除了要建立ListView本體外,還要建立ListView的欄位(Columns)
程式執行後會搜尋電腦的磁碟機數量,一個磁碟機就建一個Panel以及列示磁碟機資料夾的ListView
在ListView1被選擇的磁碟機,所屬的Panel屬性Visible會設為true,其餘的自然是false
這樣就可達到切換的效果,而且ListView改變值,不會因為切換而設回預設值