Solved

Moving a node up one place on a treeview

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now