【问题标题】:How to move Root nodes up and down in a treeview Access/VBA如何在树视图 Access/VBA 中上下移动根节点
【发布时间】:2011-11-06 08:58:56
【问题描述】:

好的。我在树视图上做了很多工作,我决定让用户可以方便地上下移动树视图的节点,但他们认为合适。我的结构只是一个简单的两级树视图,但是每个根节点都必须有一个子节点。例如:

Root
  child
  child
  child
Root
  child
  child
Root
  child
  child
  child
  child

我编写了代码,您一次只能从根节点中选中一个框。我想要做的是单击一个按钮,并让选中的根节点向上(或向下)移动一个位置(当然要带上它的子节点)。

我可以想象这种工作的唯一方法是完全重建比以前高一级的节点。当节点开始有更多的孩子等时,这似乎太耗时了。

当我搜索时,我发现了大量的 C# 结果,但由于我使用的是 VBA,它根本没有帮助我。如果有人在重建整个节点之外有任何建议,我很想听听。谢谢

【问题讨论】:

  • 我看到了这个,想知道它是否有帮助:support.microsoft.com/kb/209898
  • 你能展示一下你的表结构吗?另外,我认为重建节点实际上是一个好主意,具体取决于您如何使用此表/记录。会有成百上千的记录吗?他们会经常更换吗?如果你对两者的回答都是肯定的,那么,是的,重建可能是不可行的。否则我认为重建整个事情是正确的解决方案。
  • @Remou- 拖放示例很酷(我以前见过),但据我所知,他们正在切换节点父节点,而不是切换根节点的顺序。
  • @HK1- 我想我会重建节点然后!我想我希望有一个更优雅的解决方案

标签: ms-access treeview vba nodes


【解决方案1】:

我解决这个问题的方法是将节点的键和文本保存在临时变量中,清除键,然后切换它们。

此时,我将遍历所有子节点并将它们添加到节点数组中。我将它们每个的 nodes.Parent 属性设置为对立的节点,然后一切都完成了。

这是可能的,因为表中的数据取决于用户如何构建树形视图,所以一旦树形视图显示了他们想要的方式,我可以保存我需要的信息并在他们打开该特定记录时重建它向上。我希望这很清楚,但我在下面有一些代码块可以帮助解决这个问题。

 Private Sub MoveUP()
 Dim n As Node
 Dim key1 As String
'etc.....

 key1 = n.Key
 text1 = n.Text
 key2 = n.Previous.Key
 text2 = n.Previous.Text

'We have to clear out the keys now.
 n.Key = ""
 n.Previous.Key = ""

'Now replace the keys
 n.Key = key2
 n.Text = text2
 n.Previous.Key = key1
 n.Previous.Text = text1

 Call SwitchParents(n, n.Previous)
 End Sub

^上面的代码是将n向上移动到n.Previous点 下面的代码,是切换节点的子节点(如果有的话)

 Private Sub SwitchParents(n1 As Node, n2 As Node)
 Dim nds1() As Node 'Declare with no size to ReDim later
 Dim nds2() As Node
 Dim c As Node 'this is the child node we will use to push into the array
 Dim i As Integer 

 i = n1.Children
 ReDim nds1(0 To i)
 Set c = n1.Child
 'Notice in both loops that i remains the number of children, the arrays fill backwards
 'because when you reassign the nodes.Parent property, the node jumps to the beginning,
 'so we pack them backwards and they come out displaying the same way they did before.
 Do While Not (c Is Nothing)
   i = i - 1
   Set nds1(i) = c
   Set c = c.Next
 Loop

 i = n2.Children
 ReDim nds2(0 To i)
 Set c = n2.Child
 Do While Not (c Is Nothing)
   i = i - 1
   Set nds2(i) = c
   Set c = c.Next
Loop

If Not IsEmpty(nds1()) Then
  For i = 0 To UBound(nds1()) - 1
    Set nds1(i).Parent = n2
  Next i
End If

If Not IsEmpty(nds2()) Then
  For i = 0 To UBound(nds2()) - 1
    Set nds2(i).Parent = n1
  Next i
End If
End Sub

我希望这个例子能帮助其他任何有类似的两级树结构并希望做这样的事情的人。我是在单击按钮时执行此操作的,但是可以将其重新设计为拖放方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 2017-11-03
    • 1970-01-01
    相关资源
    最近更新 更多