treeview双击节点事件
,我想实现单击第一个子节点出现picture1,以此类推,但是添加什么名称的节点是客户自己添加的,添加完之后,单击及节点实现图片的切换,而且判断语句最好是做成下面这样子的 比如
select case TreeView1.SelectedItem.Index
case 1
picture1.visible=true
case 2
picture2.visible=true
······方便做好预留,不管别人添加多少节点都可以实现功能,节点不是无限制添加,比如最多15个
下面贴出我程序的代码(可能有好多问题啊,这是书中的代码,我修改了一下):
Option Explicit
Private Sub TrV_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then PopupMenu edit
End Sub
Private Sub t1_Click() ‘右键添加节点
Dim s As String
Dim Str As String
If trv.SelectedItem Is Nothing Then Exit Sub '如果没有选中节点则退出
s = Mid(trv.SelectedItem.Key, InStr(1, trv.SelectedItem.Key, "_") + 1)
Str = InputBox("请输入要增加的省/市名称:", "增加省/市")
If Str <> "" Then
If s = "boot" Then '为根节点表示添加省
conn.Execute "insert into sys000(bh,mc) values ('0','" & Str & "')" '将新增加的
Else '将添加的省名称保存到表中
conn.Execute "insert into sys000(bh,mc) values('" & s & "','" & Str & "')" '将添加的市名称保存到表中
End If
End If
Set rs = conn.Execute("select * from sys000")
LoadData '重新加载树
End Sub
Private Sub t2_Click() '删除节点
Dim s As String
If trv.SelectedItem Is Nothing Then Exit Sub '如果没有选中节点则退出
s = Mid(trv.SelectedItem.Key, InStr(1, trv.SelectedItem.Key, "_") + 1)
If MsgBox("确定要删除该省/市名称吗?", vbQuestion + vbYesNo) = vbYes Then '确定删除
If trv.SelectedItem.Children = 0 Then
conn.Execute "delete from sys000 where id=" & s & "" '删除节点
Set rs = conn.Execute("select * from sys000")
LoadData '重新加载树
End If
End If
End Sub
Private Sub Form_Load()
Dim Ls_Connstr As String
Ls_Connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Data.mdb;Persist Security Info=False"
conn.ConnectionString = Ls_Connstr
conn.Open
Set rs = conn.Execute("select * from sys000")
trv.ImageList = ImageList1 '设置控件的图像列表
Picture1.Picture = LoadPicture("C:\test\1.jpg")
Picture2.Picture = LoadPicture("C:\test\2.jpg")
Picture3.Picture = LoadPicture("C:\test\3.jpg")
Picture4.Picture = LoadPicture("C:\test\4.jpg")
Picture5.Picture = LoadPicture("C:\test\5.jpg")
LoadData
End Sub
Private Sub AddRootNode(Root_Key As String, str_Key As String, str_Name As String) '添加省名称
Dim ndNewNode As node
Set ndNewNode = trv.Nodes.Add(Root_Key, tvwChild, "K_" & str_Key, str_Name, 2) '2表示节点的图标
ndNewNode.Expanded = True
End Sub
Public Sub LoadData() '加载表中的信息并添加树
Dim node As node
Dim i As Integer
Dim str_Key As String
Dim str_Name As String
trv.Nodes.Clear '清除控件中的所有节点
Set node = trv.Nodes.Add(, , "boot", "中国", 1) '添加顶级节点
Do Until rs.EOF
str_Key = rs.Fields(0)
str_Name = rs.Fields(2)
If rs.Fields(1) = 0 Then
Call AddRootNode("boot", str_Key, str_Name) '增加省名称
Else
End If
rs.MoveNext
Loop
node.Expanded = True
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Sub
Private Sub trv_DblClick() '只响应叶节点(没有孩子)
If Not trv.SelectedItem Is Nothing Then
If trv.SelectedItem.Children = 0 Then
Select Case trv.SelectedItem.Key
Case 2
Picture1.Visible = True
End Select
End If
End If
End Sub
[解决办法]
不如好好看看msdn中关于 treeview 的帮助,然后按msdn给的例子写一个,然后去理解,然后再动手自己写个简单的例子。
总比直接从书上copy代码,代码写什么自己又不知道强得多啊!!