📄 treeview.frm
字号:
VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "ComDlg32.OCX"
Object = "{831FDD16-0C5C-11d2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Begin VB.Form frmTreeview
Caption = "TreeView 和 ListView"
ClientHeight = 5655
ClientLeft = 735
ClientTop = 2640
ClientWidth = 9660
Icon = "treeview.frx":0000
LinkTopic = "Form1"
ScaleHeight = 5655
ScaleWidth = 9660
Begin VB.ComboBox cmbView
Height = 315
Left = 6960
TabIndex = 4
Text = "Combo1"
Top = 0
Width = 2415
End
Begin VB.CommandButton cmdLoad
Caption = "加载"
Height = 270
Left = 2190
TabIndex = 1
Top = 4935
Visible = 0 'False
Width = 1050
End
Begin MSComCtlLib.StatusBar sbrDB
Align = 2 'Align Bottom
Height = 255
Left = 0
TabIndex = 5
Top = 5400
Width = 9660
_ExtentX = 17039
_ExtentY = 450
_Version = 393216
BeginProperty Panels {8E3867A5-8586-11D1-B16A-00C0F0283628}
NumPanels = 1
BeginProperty Panel1 {8E3867AB-8586-11D1-B16A-00C0F0283628}
EndProperty
EndProperty
End
Begin MSComCtlLib.ProgressBar prgLoad
Height = 210
Left = 255
TabIndex = 3
Top = 375
Visible = 0 'False
Width = 9090
_ExtentX = 16034
_ExtentY = 370
_Version = 393216
Appearance = 1
End
Begin MSComCtlLib.ListView lvwDB
Height = 4215
Left = 3480
TabIndex = 2
Top = 615
Width = 5880
_ExtentX = 10372
_ExtentY = 7435
LabelWrap = -1 'True
HideSelection = -1 'True
_Version = 393216
Icons = "imlIcons"
SmallIcons = "imlSmallIcons"
ForeColor = -2147483640
BackColor = -2147483643
Appearance = 1
NumItems = 0
End
Begin MSComDlg.CommonDialog dlgDialog
Left = 1605
Top = 4830
_ExtentX = 847
_ExtentY = 847
_Version = 393216
FilterIndex = 474
FontSize = 8.01821e-38
End
Begin MSComCtlLib.TreeView tvwDB
Height = 4215
Left = 255
TabIndex = 0
Top = 615
Width = 3120
_ExtentX = 5503
_ExtentY = 7435
_Version = 393216
LineStyle = 1
Style = 7
ImageList = "imlSmallIcons"
Appearance = 1
End
Begin MSComCtlLib.ImageList imlIcons
Left = 645
Top = 4800
_ExtentX = 1005
_ExtentY = 1005
BackColor = -2147483643
ImageWidth = 32
ImageHeight = 32
MaskColor = 12632256
_Version = 393216
BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628}
NumListImages = 1
BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "treeview.frx":0442
Key = "book"
EndProperty
EndProperty
End
Begin MSComCtlLib.ImageList imlSmallIcons
Left = 45
Top = 4815
_ExtentX = 1005
_ExtentY = 1005
BackColor = -2147483643
ImageWidth = 13
ImageHeight = 13
MaskColor = 12632256
_Version = 393216
BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628}
NumListImages = 6
BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "treeview.frx":075C
Key = "closed"
EndProperty
BeginProperty ListImage2 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "treeview.frx":08CE
Key = "cylinder"
EndProperty
BeginProperty ListImage3 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "treeview.frx":0A40
Key = "leaf"
EndProperty
BeginProperty ListImage4 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "treeview.frx":0BB2
Key = "open"
EndProperty
BeginProperty ListImage5 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "treeview.frx":0D24
Key = "smlBook"
EndProperty
BeginProperty ListImage6 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "treeview.frx":0FD6
Key = ""
EndProperty
EndProperty
End
Begin VB.Menu mnuFile
Caption = "文件(&F)"
Begin VB.Menu mnuLoad
Caption = "加载(&L)"
End
Begin VB.Menu mnuExit
Caption = "退出(&X)"
End
End
End
Attribute VB_Name = "frmTreeview"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private mNode As node ' 模块级节点变量。
Private mItem As ListItem ' 模块级列表项变量。
Private EventFlag As Integer ' 标记所发生的事件。
Private mCurrentIndex As Integer ' 设置标志保证这个节点没有被单击。
Private mStatusBarStyle As Integer ' 切换状态栏样式。
Private cn As ADODB.Connection ' 我们仅仅使用一个活动的连接。
Attribute cn.VB_VarHelpID = -1
Const PUBLISHER = 1 ' 对于 EventFlag, 标记发行商 colmunheader 对象。
Const TITLE = 2 ' EventFlag, 在 ListView 中标记标题
Private Sub cmdLoad_Click()
Dim intCounter As Integer ' Counter to set Progressbar.Value
Dim intIndex ' Variable for index of current node.
' 设置 ADODB 连接对象的连接字符串并且打开它。
' 创建 ADODB 记录集对象变量。
Dim rsPublishers As New ADODB.Recordset
' 打开记录集。
With rsPublishers
.Open "SELECT PubID, [Company Name] FROM Publishers", cn, adOpenStatic, adLockOptimistic
' 移动到记录尾部获得记录号,然后返回。
.MoveLast
.MoveFirst
End With
' 将进程栏设置为最大,并且使它可视。
With prgLoad
.Max = rsPublishers.RecordCount
.Visible = True
End With
' 当此记录不是最后一条记录时,添加 ListItem 对象。
' 为 ListItem 对象的文本使用 Name 字段。
Do While Not rsPublishers.EOF
intCounter = intCounter + 1
prgLoad.Value = intCounter ' 更新进程栏。
' 添加节点到 TreeView, 并且设置它的属性。
Set mNode = tvwDB.Nodes.Add(1, tvwChild, rsPublishers!pubID & " ID", CStr(rsPublishers![Company name]), "closed")
mNode.Tag = "Publisher" ' 标识此表。
' 设置变量 intIndex 到新创建的节点的 Index 属性。
' 使用此变量添加子节点对象到现在的节点。
intIndex = mNode.Index
rsPublishers.MoveNext ' 移动到下一个出版商记录。
Loop
' 隐藏进程栏
prgLoad.Visible = False
' 将状态栏样式设置为标准。
sbrDB.Style = sbrNormal
' 对出版商节点进行排序。
tvwDB.Nodes(1).Sorted = True
' 扩展顶节点。
tvwDB.Nodes(1).Expanded = True
End Sub
Private Sub cmbView_Click()
' 设置 ListView.View 属性。
lvwDB.View = cmbView.ListIndex
End Sub
Private Function FindBiblio() As String
On Error GoTo ErrHandler
' 在 Biblio.mdb 不能找到的情况下,配置命令对话框。
With dlgDialog
.DialogTitle = "不能找到 Biblio.mdb"
.Filter = "(*.MDB)|*.mdb"
End With
'如果用户点击“取消”按钮将导致错误。
dlgDialog.CancelError = True
dlgDialog.ShowOpen
Do While UCase(Right(Trim(dlgDialog.FileName), 10)) <> "BIBLIO.MDB"
MsgBox "文件名称与 BIBLIO.MDB 不符。"
dlgDialog.ShowOpen
Loop
FindBiblio = dlgDialog.FileName
Exit Function
ErrHandler:
If Err = 32755 Then
End
End If
End Function
Private Sub Form_Load()
' 首先打开全局的 Connection 对象。
On Error GoTo errFind
Set cn = New ADODB.Connection
' ConnectionString 包含数据库路径。如果
' Biblio.mdb 不存在与您的机器上,您可以在 MSDN CD 上查找。
With cn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & _
"C:\Program Files\Microsoft Visual Studio\VB98\Biblio.mdb"
.Open
End With
' Configure cmbView control.
With cmbView
.AddItem "Icon View" ' 0
.AddItem "SmallIcon View" ' 1
.AddItem "List View" ' 2
.AddItem "Report View" ' 3
.ListIndex = 3
End With
' Configure ListView control.
lvwDB.View = lvwReport
' Configure TreeView
With tvwDB
.Sorted = True
Set mNode = .Nodes.Add()
.LabelEdit = False
.LineStyle = tvwRootLines
End With
With mNode ' Add first node.
.Text = "Publishers"
.Tag = "Biblio"
.Image = "closed"
End With
frmTreeview.Show
mnuLoad_Click
Exit Sub
' 如果 Biblio 数据库不能被找到,打开
' 公用对话框控件让用户来查找它。
errFind:
If Err = -2147467259 Then
Set cn = Nothing
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & FindBiblio
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -