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
Solved

Moving a node up one place on a treeview

Posted on 2004-10-13
5
404 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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