Results 1 to 8 of 8

Thread: [RESOLVED] VB.NET - ListView And ImageList

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Resolved [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:
    1. Dim openDLG As New OpenFileDialog
    2.         Dim listItem As New ListViewItem
    3.         Dim ImageList As New ImageList
    4.  
    5.         openDLG.Title = "Select the file that you would like to upload."
    6.         openDLG.Filter = "All File Sources (*.*)|*.*"
    7.  
    8.         If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
    9.  
    10.             Try
    11.  
    12.                 Dim DirectoryName As String = New System.IO.FileInfo(openDLG.FileName).DirectoryName
    13.  
    14.                 Me.lvwAttachments.BeginUpdate()
    15.                 Me.lvwAttachments.SmallImageList = ImageList
    16.  
    17.                 ImageList.Images.Add(0, Drawing.Icon.ExtractAssociatedIcon(openDLG.FileName).ToBitmap)
    18.  
    19.                 listItem.ImageIndex = 0
    20.  
    21.                 listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).Name)
    22.  
    23.                 If DirectoryName.EndsWith("\") Then
    24.                     listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).DirectoryName)
    25.                 Else
    26.                     listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).DirectoryName & "\")
    27.                 End If
    28.  
    29.                 Me.lvwAttachments.Items.Add(listItem)
    30.  
    31.                 'AutoResize ListView Columns
    32.                 Me.lvwAttachments.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
    33.                 'Resize columns to the larger of content or header
    34.                 AdjustColumnToFill(Me.lvwAttachments)
    35.                 'Adjust Last Column To Fill ListView
    36.                 AdjustLastColumnToFill(Me.lvwAttachments)
    37.  
    38.                 Me.lvwAttachments.EndUpdate()
    39.  
    40.             Catch ex As Exception
    41.                 MessageBox.Show(ex.Message)
    42.             End Try
    43.  
    44.         End If

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    1. ImageList.Images.Add(openDLG.FileName, Drawing.Icon.ExtractAssociatedIcon(openDLG.FileName).ToBitmap)
    2. listItem.ImageKey = openDLG.FileName

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Talking Re: VB.NET - ListView And ImageList

    This code seems to work...

    vb Code:
    1. Imports System
    2. Imports System.Drawing
    3.  
    4. Public Class Form1
    5.  
    6.     Public ImageList As New ImageList()
    7.  
    8.  
    9.     Private Sub ButtonUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpload.Click
    10.  
    11.         Dim openDLG As New OpenFileDialog
    12.         Dim listItem As New ListViewItem()
    13.  
    14.  
    15.         openDLG.Title = "Select the file that you would like to upload."
    16.  
    17.         openDLG.Filter = "All File Sources (*.*)|*.*"
    18.  
    19.         If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
    20.  
    21.             Try
    22.  
    23.                 Dim ImageKey As String = Guid.NewGuid().ToString()
    24.  
    25.                 Dim DirectoryName As String = New System.IO.FileInfo(openDLG.FileName).DirectoryName
    26.  
    27.                 Me.ListView.BeginUpdate()
    28.  
    29.                 Me.ListView.SmallImageList = ImageList
    30.  
    31.                 ImageList.Images.Add(ImageKey, Drawing.Icon.ExtractAssociatedIcon(openDLG.FileName).ToBitmap)
    32.  
    33.                 listItem.ImageKey = ImageKey
    34.  
    35.                 listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).Name)
    36.  
    37.                 If DirectoryName.EndsWith("\") Then
    38.  
    39.                     listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).DirectoryName)
    40.  
    41.                 Else
    42.  
    43.                     listItem.SubItems.Add(New System.IO.FileInfo(openDLG.FileName).DirectoryName & "\")
    44.  
    45.                 End If
    46.  
    47.                 Me.ListView.Items.Add(listItem)
    48.                 'AutoResize ListView Columns
    49.                 Me.ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
    50.                 Me.ListView.EndUpdate()
    51.  
    52.                 ImageList.Images.RemoveByKey(openDLG.FileName)
    53.  
    54.             Catch ex As Exception
    55.                 MessageBox.Show(ex.Message)
    56.  
    57.             End Try
    58.  
    59.         End If
    60.  
    61.     End Sub
    62. End Class

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question Re: VB.NET - ListView And ImageList

    Hopefully I understand what you are saying...

    vb Code:
    1. ImageList.Images.Add(Drawing.Icon.ExtractAssociatedIcon(openDLG.FileName).ToBitmap)
    2.  
    3. listItem.ImageIndex = listItem.Index

    If the above is the case, listItem.ImageIndex seems to equal "-1" when stepping through the code.

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Talking Re: VB.NET - ListView And ImageList

    Quote Originally Posted by NickThissen View Post
    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

    Quote Originally Posted by NickThissen View Post
    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...

  8. #8

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width