Solved

Moving a node up one place on a treeview

Posted on 2004-10-13
5
403 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
  • 2
5 Comments
 
LVL 85

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 85

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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