|
-
Aug 19th, 2009, 03:47 AM
#1
Thread Starter
Lively Member
[RESOLVED] VB.NET - ListView And ImageList
Hi All,
I currently have a button which upon clicking allows me to add the details & icon of the selected file from the OpenFileDialog window. This all works fine but when selecting a second file, the icon from the first ListView item changes to the newly selected file but the other information remains unchanged. Below is the code I am using. Any suggestions as to what I am doing wrong?
vb Code:
Dim openDLG As New OpenFileDialog
Dim listItem As New ListViewItem
Dim ImageList As New ImageList
openDLG.Title = "Select the file that you would like to upload."
openDLG.Filter = "All File Sources (*.*)|*.*"
If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Dim DirectoryName As String = New System.IO.FileInfo(openDLG.FileName).DirectoryName
Me.lvwAttachments.BeginUpdate()
Me.lvwAttachments.SmallImageList = ImageList
ImageList.Images.Add(0, Drawing.Icon.ExtractAssociatedIcon(openDLG.FileName).ToBitmap)
listItem.ImageIndex = 0
listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).Name)
If DirectoryName.EndsWith("\") Then
listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).DirectoryName)
Else
listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).DirectoryName & "\")
End If
Me.lvwAttachments.Items.Add(listItem)
'AutoResize ListView Columns
Me.lvwAttachments.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
'Resize columns to the larger of content or header
AdjustColumnToFill(Me.lvwAttachments)
'Adjust Last Column To Fill ListView
AdjustLastColumnToFill(Me.lvwAttachments)
Me.lvwAttachments.EndUpdate()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
-
Aug 19th, 2009, 04:05 AM
#2
Re: VB.NET - ListView And ImageList
First of all turn Option Strict On. You are passing an Integer as the first argument to the Images.Add function, but there it does not accept Integers. Instead, you should be passing the Key (a String).
I'm not sure about your problem actually, but it definitely has something to do with how you set and add the image. You use the first index all the time (ImageIndex = 0), but the first image never changes; you just keep adding more images in the ImageList, but you never use them.
That does not explain why the first image would change though, that would happen if the image in the ImageList is actually changed, but I don't know how that can happen in your code; perhaps because you are using the same key, but I don't see that behavior in a test project.
I would use the FileName as the key (the full path, which will always be unique), turning the code into something like this:
vb.net Code:
ImageList.Images.Add(openDLG.FileName, Drawing.Icon.ExtractAssociatedIcon(openDLG.FileName).ToBitmap) listItem.ImageKey = openDLG.FileName
-
Aug 19th, 2009, 08:23 AM
#3
Thread Starter
Lively Member
Re: VB.NET - ListView And ImageList
This code seems to work...
vb Code:
Imports System
Imports System.Drawing
Public Class Form1
Public ImageList As New ImageList()
Private Sub ButtonUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpload.Click
Dim openDLG As New OpenFileDialog
Dim listItem As New ListViewItem()
openDLG.Title = "Select the file that you would like to upload."
openDLG.Filter = "All File Sources (*.*)|*.*"
If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Dim ImageKey As String = Guid.NewGuid().ToString()
Dim DirectoryName As String = New System.IO.FileInfo(openDLG.FileName).DirectoryName
Me.ListView.BeginUpdate()
Me.ListView.SmallImageList = ImageList
ImageList.Images.Add(ImageKey, Drawing.Icon.ExtractAssociatedIcon(openDLG.FileName).ToBitmap)
listItem.ImageKey = ImageKey
listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).Name)
If DirectoryName.EndsWith("\") Then
listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).DirectoryName)
Else
listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).DirectoryName & "\")
End If
Me.ListView.Items.Add(listItem)
'AutoResize ListView Columns
Me.ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
Me.ListView.EndUpdate()
ImageList.Images.RemoveByKey(openDLG.FileName)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
End Class
-
Aug 19th, 2009, 08:29 AM
#4
Re: VB.NET - ListView And ImageList
Why use a key at all? Why not just add the item, add the image and then set the item's ImageIndex equal to its Index? Ther will always be the same number of images and items so the indexes will always correspond.
-
Aug 19th, 2009, 09:10 AM
#5
Thread Starter
Lively Member
Re: VB.NET - ListView And ImageList
Hopefully I understand what you are saying...
vb Code:
ImageList.Images.Add(Drawing.Icon.ExtractAssociatedIcon(openDLG.FileName).ToBitmap)
listItem.ImageIndex = listItem.Index
If the above is the case, listItem.ImageIndex seems to equal "-1" when stepping through the code.
-
Aug 19th, 2009, 09:32 AM
#6
Re: VB.NET - ListView And ImageList
You did not add the item to the ListView yet, so the Index is meaningless. It only gets a value after you add it to a ListView, because only then does it know which number it is.
One thing though; what if the user deletes an item later? Then the two lists (images and items) become de-synchronized, no?
-
Aug 20th, 2009, 04:56 AM
#7
Thread Starter
Lively Member
Re: VB.NET - ListView And ImageList
 Originally Posted by NickThissen
You did not add the item to the ListView yet, so the Index is meaningless. It only gets a value after you add it to a ListView, because only then does it know which number it is.
Ah Ha...Working now 
 Originally Posted by NickThissen
One thing though; what if the user deletes an item later? Then the two lists (images and items) become de-synchronized, no?
Interesting point. I thought if you remove the image from the imagelist using the listview items index then remove the listview item, then both would re-synchronize 
Anyhow, gonna mark this thread a resolved...
-
Aug 20th, 2009, 05:02 AM
#8
Re: [RESOLVED] VB.NET - ListView And ImageList
If you remove the image as well, then yes, but that will not happen automatically so you have to remember to do that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|