VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 禁止TreeView根节点的拖放操作
发表评论(0)作者:, 平台:, 阅读:13568, 日期:2000-03-12
禁止TreeView根节点的拖放操作



一般来说我们都不希望用户把一个TreeView控件中的根节点拖放到其下的子节点中。

为了达到这一目的你首先要在TreeView控件的OLEStartDrag事件中检查拖放操作的

有效性。利用该控件的Parent属性能很快检测出你要拖放的节点是否是根节点。如

果是根节点,该属性返回一个空值(nothing)。下面是示例代码:


Option Explicit


'获取被选取的节点


Public dragNode As Node, hilitNode As Node


Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, _ x

As Single, y As Single)


Set dragNode = TreeView1.HitTest(x, y)


End Sub


'检查被选取的节点是否为根节点


Private Sub TreeView1_OLEStartDrag(Data As MSComctlLib.DataObject, _

AllowedEffects As Long)


If dragNode.Parent Is Nothing Then


Set dragNode = Nothing


End Sub


然后在OLEDragOver和OLEDragDrop事件中写入相应的条件判断语句,判断dragNode

是否为nothing,并执行相应的操作。例如在OLEDragOver事件中可用如下代码:


Private Sub TreeView1_OLEDragOver(Data As MSComctlLib.DataObject, _

Effect As Long, Button As Integer, Shift As Integer, _ x As Single, y As

Single, State As Integer)


If Not dragNode Is Nothing Then


TreeView1.DropHighlight = TreeView1.HitTest(x, y)


End If


End Sub