怎么判断Treeview某个子节点存在?

hhfh 2006-03-09 04:22:33
Set nodX = tvTreeView.Nodes.Add(, , "P01" , "父级1")
Set nodX = tvTreeView.Nodes.Add("P01", tvwChild, "C101", "子级1")
Set nodX = tvTreeView.Nodes.Add("P01", tvwChild, "C102", "子级2")
.....
Set nodX = tvTreeView.Nodes.Add(, , "P02" , "父级2")
Set nodX = tvTreeView.Nodes.Add("P02", tvwChild, "C201", "子级1")
Set nodX = tvTreeView.Nodes.Add("P02", tvwChild, "C202", "子级2")
.....
Private Sub tvTreeView_Expand(ByVal Node As MSComctlLib.Node)
我点击"父级2"节点 然后判断其子节点是否有"C203"子节点存在
有就删除
这里代码怎么写?

End if
...全文
171 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhfh 2006-03-10
  • 打赏
  • 举报
回复
我现用On Error Resume Next 来跳过错误
Private Sub tvTreeView_Expand(ByVal Node As MSComctlLib.Node)
Dim Rst_Child As New ADODB.Recordset '子级
Dim Cmd_Child As New ADODB.Command
Dim StrNodeC As String '子节点
'添加 Node 对象。
Dim nodx As Node '声明 Node 变量。
'MsgBox Node.Child

With Cmd_Child

.ActiveConnection = Cn
.CommandType = adCmdStoredProc
.CommandText = "sp_Product_ChildList"
.Parameters("@FCLBH") = Right(Node.Key, 2)

Set Rst_Child = .Execute
End With

StrNodeC = Replace(Node.Key, "P", "C") 'P01=>C01
On Error Resume Next 'StrNodeC不存在 Remove会错误
Call tvTreeView.Nodes.Remove(StrNodeC)

Do While Not Rst_Child.EOF
Set nodx = tvTreeView.Nodes.Add(Node.Key, tvwChild, "C" & Rst_Child!FCLBH, Rst_Child!FCLMC)
Rst_Child.MoveNext
Loop
Rst_Child.Close

Set Rst_Child = Nothing
Set Cmd_Child = Nothing
End Sub
hhfh 2006-03-10
  • 打赏
  • 举报
回复
Set nodx = tvTreeView.Nodes.Item("C202") //如果本来没有 这里运行就错误了
If nodx.Parent.Key = "P02" Then
MsgBox "Ok"
End If
zuoxingyu 2006-03-09
  • 打赏
  • 举报
回复
Private Sub Form_Load()
Dim nodx As Node
Set nodx = tvTreeView.Nodes.Add(, , "P01", "¸¸¼¶1")
Set nodx = tvTreeView.Nodes.Add("P01", tvwChild, "C101", "×Ó¼¶1")
Set nodx = tvTreeView.Nodes.Add("P01", tvwChild, "C102", "×Ó¼¶2")

Set nodx = tvTreeView.Nodes.Add(, , "P02", "¸¸¼¶2")
Set nodx = tvTreeView.Nodes.Add("P02", tvwChild, "C201", "×Ó¼¶1")
Set nodx = tvTreeView.Nodes.Add("P02", tvwChild, "C202", "×Ó¼¶2")
End Sub

Private Sub tvTreeView_Expand(ByVal Node As MSComctlLib.Node)
'ÎÒµã»÷"¸¸¼¶2"½Úµã È»ºóÅÐ¶ÏÆä×Ó½ÚµãÊÇ·ñÓÐ"C203"×Ó½Úµã´æÔÚ
''ÓоÍɾ³ý
'ÕâÀï´úÂëÔõôд?
If Node.Key = "P02" Then
Dim nodx As Node
Set nodx = tvTreeView.Nodes.Item("C202")
If nodx.Parent.Key = "P02" Then
MsgBox "Ok"
End If
End If
End Sub
KissApple 2006-03-09
  • 打赏
  • 举报
回复
单击父级2后,得到p02,然后再从数据库中删除所有以p02为父节点的节点

1,453

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧