从动态树视图中选择节点

时间:2016-04-28 05:12:43

标签: vb.net dynamic treeview qtreeview

我在运行时创建一些树视图,需要获取在其中一个树视图中选择的节点的值。我怎样才能做到这一点。谷歌搜索没有返回任何有用的答案。

1 个答案:

答案 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

我正在使用上下文菜单弹出允许我将节点连同附加到其他位置的数据一起移动。数据处理正在excel中完成,所以我需要获取节点的所有信息。

我现在面临的问题是,我无法直接右键单击某个节点以获取上下文条,如果已完成第一个节点被选中而不是被点击的节点如何解决此问题。