在Visual Basic 6.0 中,有那么一个bug ,遇上了也郁闷。
微软官方描述:http://support.microsoft.com/kb/221557
简而言之,当你设置TreeView中Node 的Checked 属性为True,或者在按下左键拖拉到另一个Node时,
并不发生NodeClick事件。
解决方法,自然可以参考官方的做法,本人也试过,只是没有并没有做自己想要的效果,所以自己写
一个解决方案。
我遇上的问题有两个。
1.node.Checked = True,却没有看到如期效果,CheckBox的勾一样没有勾上。
解决方法:因我的TreeView控件,有绑定ImageList控件,查找一些资料,不能在设计时进行绑定,
于是多加了一句代码,TreeView1.ImageList = ImageList1,把绑定延迟到运行时
问题解决了,具体什么理由,不详。
2.在一个node的Checkbox 上按左键,拖动到另一node,两个起始的node都会被选定。
而我想要的效果是:当满足条件时才勾选,其他情况不勾选。
勾选动作可分为三个事件,mouseDown,NodeCheck,mouseUp,于是就有了如下:
Priavte nodDown As Node
Pivate nodCheck As Node
'mouseDown Event
Private Sub tvwCtrl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (Button = vbLeftButton) then
If Not condition Then
Set nodDown = tvwCtrl.HitTest(X, Y)
If nodDown Is Nothing Then Exit Sub
End if
End If
End Sub
Private Sub tvwCtrl_NodeCheck(ByVal Node As MSComctlLib.Node)
If condition Then
'''do something
Else
Set nodCheck = Node
End If
End Sub
‘
Private Sub tvwCtrl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (Button = vbLeftButton) Then
If Not condition Then
If nodCheck Is Nothing Then
Else
If nodCheck.key <> nodDown.key Then
nodDown.Checked = Not nodDown.Checked
Else
nodCheck.Checked = Not nodCheck.Checked
End If
Set nodCheck = Nothing
End If '
End If 'buttons
Set nodDown = Nothing
End if
End Sub
综上,自己纠结了好半天,书以记之。
本文介绍了解决VisualBasic6.0中TreeView控件的两个常见问题:设置节点的Checked属性无效以及节点拖拽时出现的选择错误。通过调整绑定方式及自定义鼠标事件处理实现了预期效果。

5633

被折叠的 条评论
为什么被折叠?



