-1

I am creating some treeviews at runtime and need to get value of the nodes that have been selected in one of these treeviews. How can I accomplish this. A google search did not return any useful answers.

  • 1
    Hello and welcome to Stack Overflow! You have to supply us with some code, and a more thorough explanation of what you want and what this "value" is that you want to get. A good read is also [How to ask](http://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Visual Vincent Apr 28 '16 at 05:27
  • A `TreeView` is a `TreeView`, regardless of whether you add it to the form in the designer or create it at run time. Once you have a reference to that `TreeView`, you only use it one way. That simply means that, when you create the `TreeView` at run time, you need to keep a reference to it somewhere that you can access later on. That might be in a `List(Of Treeview)` or something else, depending on what is most appropriate for your situation. – jmcilhinney Apr 28 '16 at 05:32
  • ok so i was able to solve the problem of getting the node's text (value) earlier asked. the code used is shown below but now i have a new problem. also stated below – Utsav Mudgal Apr 29 '16 at 06:44

1 Answers1

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

I am using a context menu to popup t allow me to shift node along with the data attached to it to another location. the data handling is being done in excel so i needed to get all the information of the node.

the problem that i face now is that i am not able to directly right click on a node to get the context strip if done the first node gets selected and not the one that was clicked how can i resolve this issue.