Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Move a nodewith its subnode up/down in a treeview

Hi,

For example a treeview:

A
B
C
..D
....E
F

I would like to move for example C and its subnode just after A with a click of a button.  I'm already configured for drag and drop so I can change parent, but I just can't figure out how to move higher or lower (personalised sorting)

Thanks,

Martin
0
vicmart69
Asked:
vicmart69
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try this app out:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TreeView1 = New System.Windows.Forms.TreeView
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TreeView1
        '
        Me.TreeView1.CheckBoxes = True
        Me.TreeView1.ImageIndex = -1
        Me.TreeView1.Location = New System.Drawing.Point(8, 8)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.SelectedImageIndex = -1
        Me.TreeView1.Size = New System.Drawing.Size(272, 248)
        Me.TreeView1.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(288, 104)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(88, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Up"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(288, 136)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(88, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Down"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(384, 266)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TreeView1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim tn As TreeNode
        Dim n As Integer
        For i = 1 To 5
            n = n + 1
            TreeView1.Nodes.Add("Node" & n)
            For j = 1 To 3
                n = n + 1
                tn = TreeView1.Nodes(i - 1).Nodes.Add("Node" & n)
                For k = 1 To 3
                    n = n + 1
                    tn.Nodes.Add("Node" & n)
                Next
            Next
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim curNode As TreeNode = TreeView1.SelectedNode
        If Not (curNode Is Nothing) Then
            Dim prevNode As TreeNode = curNode.PrevNode
            If Not (prevNode Is Nothing) Then
                TreeView1.Nodes.Remove(curNode)
                If Not (prevNode.Parent Is Nothing) Then
                    prevNode.Parent.Nodes.Insert(prevNode.Index, curNode)
                Else
                    TreeView1.Nodes.Insert(prevNode.Index, curNode)
                End If
                TreeView1.SelectedNode = curNode
                TreeView1.Focus()
            Else
                TreeView1.Focus()
            End If
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim curNode As TreeNode = TreeView1.SelectedNode
        If Not (curNode Is Nothing) Then
            Dim nextNode As TreeNode = curNode.NextNode
            If Not (nextNode Is Nothing) Then
                TreeView1.Nodes.Remove(nextNode)
                If Not (curNode.Parent Is Nothing) Then
                    curNode.Parent.Nodes.Insert(curNode.Index, nextNode)
                Else
                    TreeView1.Nodes.Insert(curNode.Index, nextNode)
                End If
                TreeView1.SelectedNode = curNode
                TreeView1.Focus()
            Else
                TreeView1.Focus()
            End If
        End If
    End Sub
End Class
0
 
arif_eqbalCommented:
Just use

        Dim ND As TreeNode
        ND = TreeView1.Nodes(2) 'Index of Node to Move
        TreeView1.Nodes.RemoveAt(2)
        TreeView1.Nodes.Insert(0, ND) 'Index Where to Move
0
 
vicmart69Author Commented:
Works perfectly.

I first tried arif eqbal solution on a blank project and it worked perfectly.  however I haven't been able to make it work in my program (??)  .. always had "argument is not valid value: index".  So I went for Idle Mind solution and it worked.

Your help is greatly appreciated.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now