2011-08-29 30 views
0

好的。我一直在樹視圖上工作很多,我決定允許用戶上下移動樹視圖的節點,不過他們認爲合適。我的結構只是一個簡單的兩級樹視圖,但是每個根節點都必須有一個子節點。例如:如何在樹視圖中上下移動根節點Access/VBA

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

我寫了代碼,您只能從根節點一次檢查一個框。我想要做的是點擊一個按鈕,並讓檢查過的根節點向上(或向下)移動一個位置(當然要帶着它的孩子)。

我可以想象這個工作的唯一方法是完全重建比先前高一級的節點。這似乎是當節點開始有更多的孩子等太費時。

當我搜索時,我發現了大量的C#結果,但因爲我使用VBA它根本沒有幫助我。如果有人在重建整個節點之外有任何建議,我很樂意聽到它。謝謝

+0

我看到了這個,並想知道它是否可能有所幫助:http://support.microsoft.com/kb/209898 – Fionnuala

+2

你可以顯示你的表結構嗎?另外,我認爲重建節點實際上是一個好主意,具體取決於你如何使用這個表/記錄。會有成百上千的記錄嗎?他們會經常改變嗎?如果你對兩者都回答「是」,那麼,是的,重建可能是不可行的。否則我認爲重建整個事情是正確的解決方案。 – HK1

+0

@ Remou-這個拖放示例很酷(我以前見過它),但從我收集的內容來看,它們是交換節點父項,而不是切換根節點的順序。 – misterManager

回答

0

我解決這個問題的方法是將我的節點的鍵和文本保存在臨時變量中,清除鍵,然後切換它們。

此時,我循環遍歷所有孩子並將它們添加到節點數組中。我將它們中的每一個的nodes.Parent屬性設置爲相對節點,然後完成了大部分工作。

這是可能的,因爲表中的數據取決於用戶如何構建treeview,所以一旦他們有樹形視圖顯示他們想要的方式,我可以保存我需要的信息並在他們打開特定記錄時重建它備份。我希望這是明確的,但我有一些代碼塊以幫助解決這個問題。

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.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 

希望這個例子將會幫助任何擁有類似兩級樹結構的人,並且正在尋找像這樣的東西。我是通過點擊按鈕來完成的,但是它可以通過拖放方法進行修改。