2016-04-28 123 views
-1

我在運行時創建了一些樹視圖,並需要獲取在其中一個樹視圖中選擇的節點的值。我怎樣才能做到這一點。谷歌搜索沒有返回任何有用的答案。動態樹視圖中的選擇節點

+1

你好,歡迎來到Stack Overflow!你必須向我們提供一些代碼,並且更全面地解釋你想要什麼以及你想要獲得什麼樣的「價值」。好的閱讀也是[How to ask](http://stackoverflow.com/help/how-to-ask)和[如何創建一個最小,完整和可驗證的示例](http://stackoverflow.com/幫助/ MCVE)。 –

+0

無論您將其添加到設計器中的表單還是在運行時創建它,「TreeView」都是一個「TreeView」。一旦你有了對'TreeView'的引用,你只能用它一種方式。這僅僅意味着,當你在運行時創建'TreeView'時,你需要在後面的某個地方保留對它的引用。這可能在'List(TreeView)'或其他東西,取決於什麼是最適合你的情況。 – jmcilhinney

+0

好吧,所以我能夠解決獲得節點的文本(價值)問題的問題。 使用的代碼如下所示,但現在我有一個新問題。 也在下面陳述 –

回答

0
Private Sub line_balancing_context_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles line_balancing_context.Opening 
    Dim wksnum1 As Integer = 0 
    Dim wksact As Integer = 0 
    Dim wkstations1() As String = Nothing 
    Dim wksactivity() As String = Nothing 
    Dim currentsheet As String 
    Dim stationtree1 As TreeView 
    Dim actname As TreeNode = Nothing 
    Dim item As ContextMenuStrip 
    Dim subitem() As ToolStripDropDownButton 
    For Each Control In display_splitcontainer.Panel1.Controls 
     If TypeOf Control Is TreeView Then 
      If MouseIsOverControl(Control) = True Then 
       stationtree1 = New TreeView 
       stationtree1 = Control 
       startsheet = stationtree1.Name 
      End If 
     End If 
    Next 
    line_balancing_context.Items.Clear() 
    osheet = obooks.ActiveSheet 
    currentsheet = osheet.Name 
    osheet = obooks.Worksheets("Relations Form") 
    orng = osheet.Range("A1") 
    Do Until orng.Value Is Nothing 
     orng = orng.Offset(, 1) 
     wksnum1 = wksnum1 + 1 
     ReDim Preserve wkstations1(0 To wksnum1) 
     wkstations1(wksnum1 - 1) = orng.Value 
    Loop 
    For Me.i = 0 To wkstations1.Length - 1 
     If Not wkstations1(i) Is Nothing Then 
      line_balancing_context.Items.Add(wkstations1(i)) 
     End If 
    Next 

    osheet = obooks.Worksheets(currentsheet) 
    osheet.Activate() 
End Sub 
Private Sub linebalancingcontext_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles line_balancing_context.ItemClicked 
    sheetname = e.ClickedItem.Text 
    Dim stationtree1 As TreeView 
    Dim actname As TreeNode = Nothing 
    Dim stationtree2 As TreeView 
    For Each Control In display_splitcontainer.Panel1.Controls 
     If TypeOf Control Is TreeView Then 
      stationtree1 = New TreeView 
      stationtree1 = Control 
      If stationtree1.Name = startsheet Then 
       If Not stationtree1.SelectedNode Is Nothing Then 
        actname = stationtree1.SelectedNode 
        actname.Remove() 
        Exit For 
       End If 
      End If 
     End If 
    Next 

    For Each Control In display_splitcontainer.Panel1.Controls 
     If TypeOf Control Is TreeView Then 
      stationtree2 = New TreeView 
      stationtree2 = Control 
      If stationtree2.Name = sheetname Then 
       If stationtree2.Nodes.ContainsKey(actname.Text) Then 
        actname.Remove() 
       End If 
       stationtree2.Nodes.Add(actname) 
       Exit For 
      End If 
     End If 
    Next 
    If MTM_analyser.worksheetexists(startsheet & "M") Then 

     osheet = obooks.Worksheets(startsheet & "M") 
     osheet.Activate() 
     With osheet.UsedRange 
      orng = .Find("Description of Action") 
      col1 = orng.Column 
      orng = .Find("Parent Activity") 
      col2 = orng.Column 
     End With 
     orng = osheet.Range(Chr(64 + col1) & ":" & Chr(64 + col1)).Find(actname.Text) 
     If Not orng Is Nothing Then 
      parent1 = CType(osheet.Cells(orng.Row, col2), excel.Range).Value 
      Dim some As String = "A" & orng.Row & ":" & "XFD" & orng.Row 
      osheet.Range(some).Cut() 
      object1 = osheet.Range(some) 
      osheet2 = obooks.Worksheets(sheetname & "M") 
      osheet2.Activate() 
      row1 = lastrowmtm(sheetname & "M") 
      If parent1 = startsheet Then 

       Dim some1 As String = "A" & row1 & ":" & "XFD" & row1 
       If Not object1 Is Nothing Then 
        osheet2.Range(some1).Insert(, object1) 
       End If 
       CType(osheet.Cells(row1, col2), excel.Range).Value = sheetname 
       object1 = Nothing 
      End If 
     End If 
    End If 
    transferactivity(actname) 
End Sub 
Sub transferactivity(ByVal aTreeNode As TreeNode) 
    Dim n As TreeNode 
    sheetnum = 0 
    For Each n In aTreeNode.Nodes 
     transferRecursive(n) 
    Next 
End Sub 
Private Sub transferRecursive(ByVal n As TreeNode) 
    System.Diagnostics.Debug.WriteLine(n.Text) 
    If MTM_analyser.worksheetexists(startsheet & "M") Then 
     osheet = obooks.Worksheets(startsheet & "M") 
     osheet.Activate() 
     With osheet.UsedRange 
      orng = .Find("Description of Action") 
      col1 = orng.Column 
      orng = .Find("Parent Activity") 
      col2 = orng.Column 
     End With 
     orng = osheet.Range(Chr(64 + col1) & ":" & Chr(64 + col1)).Find(n.Text) 
     If Not orng Is Nothing Then 
      parent1 = CType(osheet.Cells(orng.Row, col2), excel.Range).Value 
      Dim some As String = "A" & orng.Row & ":" & "XFD" & orng.Row 
      osheet.Range(some).Cut() 
      object1 = osheet.Range(some) 
      osheet2 = obooks.Worksheets(sheetname & "M") 
      osheet2.Activate() 
      If parent1 = startsheet Then 
       MsgBox("woaah") 
      Else 
       orng2 = osheet2.Range(Chr(64 + col1) & ":" & Chr(64 + col1)).Find(parent1) 
       osheet2.Range("A" & orng2.Row + 1 & ":" & "XFD" & orng2.Row + 1).Insert(, object1) 
       osheet.Range(some).EntireRow.Delete() 
      End If 
     End If 
    End If 
    Dim aNode As TreeNode 
    For Each aNode In n.Nodes 
     transferactivity(aNode) 
    Next 
End Sub 

我正在使用上下文菜單彈出t允許我將節點以及附加到它的數據轉移到另一個位置。數據處理正在Excel中完成,因此我需要獲取節點的所有信息。

我現在面對的問題是,我不能直接右鍵單擊節點以獲取上下文條,如果完成了第一個節點被選中,而不是被點擊的節點,我該如何解決這個問題。