Solved

re-order a listview containg images from an imagelist using drag and drop

Posted on 2004-08-26
6
798 Views
Last Modified: 2012-08-14
I have an imagelist containg images lnked to a listview which displays the images contained in the imagelist.

Ideally i want to be able to select an image in the listview and move it up or down in the hierachy of the listview and drop it a new location in the listview.  

...Or alternatively use up and down buttons to move a selected image up or down the hierachy.

Sample code appreciated.

thanks
0
Comment
Question by:tcarmich
  • 2
  • 2
6 Comments
 
LVL 8

Expert Comment

by:wguerram
ID: 11912718
Just send the listview object and the number of the steps the selected item should move

Negative number in the steps will move the item up
Positive number in the steps will move the item down

Sub MoveSelectedItem(ByVal [ListView] As ListView, ByVal Steps As Integer)
        Dim item As ListViewItem
        Dim index As Integer
        Dim count As Integer

        With [ListView]
            count = .Items.Count
            If count > 0 Then
                item = .SelectedItems.Item(0).Clone
                index = .SelectedItems.Item(0).Index
               
                If index = 0 And Steps <= 0 Or index = count - 1 And Steps > 0 Then
                    item = Nothing
                    Exit Sub
                End If

                .Items.RemoveAt(index)

                index = index + Steps
                If index < 0 Then
                    index = 0
                ElseIf index > count - 1 Then
                    index = count - 1
                End If

                .Items.Insert(index, item.Clone).Selected = True
                item = Nothing
            End If
        End With
    End Sub

Hope this help.
0
 
LVL 8

Expert Comment

by:wguerram
ID: 11915584
Did this worked for you?

or You mean moving just the images and leave the items in its placed?

The previous example will work only the listview is not ordered.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11932994
Here is a small app that allows you to move the Images around in a ListView.  Just pick a folder that has pictures in it using the "Select Folder" button and then drag the images around.

Regards,

Idle_Mind

Imports System
Imports System.IO
Imports System.Windows.Forms

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 Button1 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents FolderBrowserDialog1 As System.Windows.Forms.FolderBrowserDialog
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog
        Me.ListView1 = New System.Windows.Forms.ListView
        Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(80, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Select Folder"
        '
        'Label1
        '
        Me.Label1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label1.Location = New System.Drawing.Point(96, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(368, 24)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Label1"
        '
        'ListView1
        '
        Me.ListView1.AllowDrop = True
        Me.ListView1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ListView1.Location = New System.Drawing.Point(8, 40)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(456, 304)
        Me.ListView1.TabIndex = 3
        '
        'ProgressBar1
        '
        Me.ProgressBar1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ProgressBar1.Location = New System.Drawing.Point(8, 40)
        Me.ProgressBar1.Name = "ProgressBar1"
        Me.ProgressBar1.Size = New System.Drawing.Size(456, 8)
        Me.ProgressBar1.TabIndex = 4
        Me.ProgressBar1.Visible = False
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(472, 350)
        Me.Controls.Add(Me.ProgressBar1)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.ListView1)
        Me.Name = "Form1"
        Me.Text = "Browse Images via Thumbnails in a ListView"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Const thumbWidth As Byte = 50
    Private Const thumbHeight As Byte = 50
    Private sourceItem As ListViewItem

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = Environment.CurrentDirectory & "\"
        updateImages()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            Label1.Text = FolderBrowserDialog1.SelectedPath
            updateImages()
        End If
    End Sub

    Private Sub updateImages()
        Dim fiTemp As FileInfo
        Dim thumbEntry As ListViewItem
        Dim imageList1 As ImageList = New ImageList
        Dim di As New DirectoryInfo(Label1.Text)
        Dim fi As FileInfo() = di.GetFiles()
        Dim p As Integer, i As Integer, numFiles As Integer = fi.Length
        Dim myCallback As Image.GetThumbnailImageAbort = _
                New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

        ListView1.Items.Clear()
        imageList1.ImageSize = New Size(thumbWidth, thumbHeight)
        ListView1.LargeImageList = imageList1
        ListView1.SmallImageList = imageList1

        ListView1.BeginUpdate()
        ProgressBar1.Value = 0
        ProgressBar1.Visible = True
        i = 0
        For Each fiTemp In fi
            If fiTemp.Extension.ToUpper.Equals(".BMP") Or _
                    fiTemp.Extension.ToUpper.Equals(".JPG") Or _
                    fiTemp.Extension.ToUpper.Equals(".GIF") Then
                Try
                    imageList1.Images.Add(Image.FromFile(fiTemp.FullName).GetThumbnailImage(thumbWidth, thumbHeight, myCallback, IntPtr.Zero))
                    thumbEntry = New ListViewItem
                    thumbEntry.Text = fiTemp.Name
                    thumbEntry.ImageIndex = imageList1.Images.Count - 1
                    ListView1.Items.Add(thumbEntry)
                Catch ex As Exception
                    MsgBox(fiTemp.FullName & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Information, "Error Loading Image")
                End Try
            End If
            i = i + 1
            p = CInt(i / numFiles * 100)
            ProgressBar1.Value = p
            Application.DoEvents()
        Next fiTemp
        ProgressBar1.Visible = False
        ListView1.EndUpdate()
    End Sub

    Private Function ThumbnailCallback() As Boolean
        Return False
    End Function

    Private Sub ListView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag
        sourceItem = e.Item
        DoDragDrop(e.Item, DragDropEffects.Move)
    End Sub

    Private Sub ListView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragOver
        e.Effect = DragDropEffects.Move
        Dim clientPoint As Point = ListView1.PointToClient(New Point(e.X, e.Y))
        Dim over As ListViewItem = ListView1.GetItemAt(clientPoint.X, clientPoint.Y)
        Dim temp As ListViewItem

        Dim tempText As String
        Dim tempImageIndex As String

        If Not (over Is Nothing) And Not (over Is sourceItem) Then
            tempText = over.Text
            tempImageIndex = over.ImageIndex

            over.Text = sourceItem.Text
            over.ImageIndex = sourceItem.ImageIndex

            sourceItem.Text = tempText
            sourceItem.ImageIndex = tempImageIndex

            sourceItem = over
        End If
    End Sub

End Class
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12196454
I don't know why the author never closed this question after putting this in my feedback:
http://www.experts-exchange.com/viewMemberFeedback.jsp?mid=1539809

Author: tcarmich
Date: 08/31/2004 07:05AM MDT
View Source Question
Post a Reply to this feedback

Thanks, this is exactly what I was trying to do.
0
 

Author Comment

by:tcarmich
ID: 12198388
Sorry my fault entirely,  I was distracted to something else,  and I didnt realise that I should have closed it ... Sorry

It was a really good answer, but then I discobered that the Listview doesnt scroll when dragging ... aaaghhh!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Retain selection in datagridview 2 22
Form design in vb.net 7 18
VB.NET HttpWebRequest 12 30
Format column on datatable 7 17
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

17 Experts available now in Live!

Get 1:1 Help Now