用vb和数据库做系统时,在主窗体上想做一个树状目录,用treeview和image两个控件要怎么做啊
TreeView 中要使用图片,一般和 ImageList 控件结合.
如下图:
附件中包含了Form8 及所要用的Access数据库db8.mdb
重要的程序有:
1、Form_Load() 中设置数据控件Data 及给TreeView 加载数据:
Private Sub Form_Load()
Dim snRes As DAO.Recordset
Dim N As Node
dataName = App.Path + "\db8.mdb"
With fData
.DatabaseName = dataName
.Connect = "Access"
.RecordSource = "SELECT * FROM T3011 ORDER BY F0,F1 "
.Refresh
End With
DoEvents
Set snRes = fData.Database.OpenRecordset("SELECT F1,F2 FROM T301 WHERE F1<>'CMPY'", dbOpenSnapshot)
TreeLB.Nodes.Clear
Set N = TreeLB.Nodes.Add(, , "R", "分店资料", "ImgR")
Do While Not snRes.EOF
Set N = TreeLB.Nodes.Add("R", tvwChild, "I_" + Trim(snRes(0)), Trim(snRes(1)), "ImgS")
snRes.MoveNext
Loop
TreeLB.Nodes(1).Expanded = True
End Sub
2、当点击TreeView 时系统找到对应的“分店营业员”资料给Data控件显示:
Private Sub TreeLB_NodeClick(ByVal Node As MSComctlLib.Node)
On Error GoTo NodeClickErr
Dim MySQL$
If InStr(Node.Key, "R") > 0 Then
fData.RecordSource = "SELECT * FROM T3011 ORDER BY F1"
fData.Refresh
DoEvents
Exit Sub
ElseIf InStr(Node.Key, "I_") > 0 Then
fData.RecordSource = "SELECT * FROM T3011 WHERE F0='" + Trim(Mid$(Trim(Node.Key), InStr(Node.Key, "I_") + 2)) + "' ORDER BY F1"
fData.Refresh
DoEvents
Exit Sub
ElseIf InStr(Node.Key, "C_") > 0 Then
MySQL$ = "F0='" + Mid$(Node.Key, 1, InStr(Node.Key, "C_") - 1) + "' "
MySQL$ = MySQL$ + "AND F1='" + Trim(Mid$(Trim(Node.Key), InStr(Node.Key, "C_") + 2)) + "' "
fData.Recordset.FindFirst MySQL$
DoEvents
End If
Exit Sub
NodeClickErr:
MsgBox "记录下面错误信息:" + Chr(13) + Error$, 48, "重要提示"
Err = 0
Exit Sub
End Sub
详细资料下载后运行并研究....
2013-12-03