|
有关于TreeView拖拽移动节点的代码中:
' 开始拖动时,设定选择为空,已备选择数据
Private Sub objTreeview_OLEStartDrag(Data As Object, AllowEffects As Long)
Me!objTreeview.Object.SelectedItem = Nothing 这里设置了选中为空
End Sub
-------------------------------------------------------------------------------------------
' 拖动鼠标事件,选择拖放节点,并高亮度显示放开Mouse时接受数据的节点,本程序设置已选节点为已拖动过,这样,程序将不再重置它,后半部分程序高亮显示结果
Private Sub objTreeView_OLEDragOver (Data As Object,Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single, State As Integer)
Dim oTree As TreeView
set oTree = Me!objTreeView.Object
'如无节点被选中,则选择你曾经拖过的第一个
If oTree SelectedItem Is Nothing Then
Set oTree SelectedItem = oTree.HitTest(X,Y)
End If
' 高亮显示
Set oTree.DropHighlight = oTree.HitTest(x, y)
----------------------------------------------------------------------------------------------
'移动节点在TreeView空间中 的位置,并更改数据表记录。程序先检查是否存在一个已选节点,并继续检查是否高亮显示放下对象,如果未高亮显示,表示用户将其放入了空白区域,则将其赋值为根节点
Private Sub objTreeView_OLEDragDrop( Data as object,Effect As Long, Button As Integer,Shift As Integer, x as single, y as single)
on error goto Err_ObjTreeView_OLEDragdrop
Dim oTree as TreeView, strKey As String, StrText As String
Dim nodNow As Node, nodDragged As Node
Dim db as Database, rs as Recordset
set db = CurrentDb
'打开表进行编辑
Set rs = db.OpenRecordset("tblBOM",adOpenDynaset)
Set oTree = Me!objTreeView.Object
'如误解点被选中,则下课休息
If oTree SelectedItem is Nothing Then
Else '相反
Set nodDragged = oTree.SelectedItem 但是这里又有SelectedItem了。之前不是已经设置为空了吗?为什么还能有?
'如果节点被拖放到空白区,则修改数据表并将其设为根节点
If oTree.DropHighLight Is Nothing Then
'保存Text值及显示文本以备重新装入时使用
strKey = nodDragged.Key
strText = nodDragged.Text
' 删除当前对象的节点
oTree.Nodes.Remove NodDragged.Index
'在数据表中查找记录并修改之
rs.FindFirst "FBOMID=" & Mid(strKey,2)
rs.Edit
rs!FMainID = Me.FMaterial
rs.Update
'将其装入为根节点
Set nodNew = oTree.Nodes.Add( , , strKey,StrText,1,2)
'装入其所有子节点
AddChild nodNew,rs
ElseIf nodDragged.Index<> oTree.DropHighLight.Index Then
Set nodeDragged.Parent = oTree.DropHighLight
rs.findFirst "[ID]=" & mid(nodDragged.Key,2)
rs.Edit
rs!MainID = Mid(oTree.DropHighlight.Key, 2)
rs.Update
End If
End IF
'取消选择
Set NodDragged = Nothing
'取消高亮
Set oTree.DropHIghLight = Nothing
Exit_objTreeVeiw_OLEDrapDrop:
Exit Sub
Err_objTreeView_OLEDrapDrop:
If Err.Number = 35614 Then
MsgBox "母件不得直接成为子件的子件!“
Else
MsgBox "节点移动错误!"
End If
Resume Exit_objTreeVeiw_OLEDrapDrop
=======================================================
在OLEStartDrag事件中明明已经设置SeletedItem为Nothing了,为什么OLEDragDrop 事件中还能取得SelecteItem的变量?
我看着的一脸茫然。请大神帮个忙指点下迷津。
在此先谢过了。
|
|