Solved

Moving a node up one place on a treeview

Posted on 2004-10-13
5
406 Views
Last Modified: 2008-06-28
How do you move a node up or down one place on a treeview in vb.net
0
Comment
Question by:visualbasic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12299448
You need to define the behaviour of your nodes a little clearer.

Do you want the node to stay on the same level or can it change depth?  Should it only move up/down swapping with the next node at the same depth?

A
+-B
|  +-C
|  +-D
+-E
| +-F
| +-G
+-H
  +-I
  +-J

In the tree above, if you move B down, should it swap with C or should it swap with E?
Should D be able to move down?  or should it swap with E or F?

~IM
0
 

Author Comment

by:visualbasic
ID: 12306672
Hi
if you move B down then It should swap with E and if you move G up it should swap with F only.

Cheers
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 12312434
The following allows you to move the current node up/down in the treeview while maintaining its depth:

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question