Listview Refuses to get image

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 820 Views - Last Post: 07 September 2015 - 06:39 PM Rate Topic: -----

#1 Pi_User5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 06-April 15

Listview Refuses to get image

Posted 06 September 2015 - 09:15 AM

In my program, I have a form called browser and in that form, there is a Listview called ListView1 and an image list called shellimages32. In the image list, there is a folder image at item 0. Posted Image I then have a function file called shellbrowser.vb and my code used to list files and folders are below:

Public Sub BrowseSystem()
        browser.ListView1.Items.Clear() 'Clear previous files and folders
        browser.shellimages16.Images.Clear() 'Clear previous icons to save memory
        browser.shellimages32.Images.Clear() 'Clear previous icons to save memory
        Dim path As String = browser.ToolStripComboBox1.Text 'Get path from address box
        browser.ListView1.Focus() 'Focus to listbox
        My.Settings.lastfolder = path 'Save to settings current folder
        Call BrowseComputer()
        If Directory.Exists(path) Then 'Check if folder exists
            Try
                browser.ToolStripComboBox1.Text = path 'Update the address bar
                Dim treenode As New TreeNode(path)
                browser.TreeView1.Nodes.Add(treenode)
                browser.TreeView1.SelectedNode = treenode
                For Each dirname As String In IO.Directory.GetDirectories(path) 'Loop through folders in the folder
                    Dim lvi As ListViewItem = browser.ListView1.Items.Add(IO.Path.GetFileName(dirname), imageIndex:=0)
                Next
            Catch ex As Exception
            End Try
        End If
        Try
            Dim di As New IO.DirectoryInfo(path) 'Folder being browsed
            Dim diar1 As IO.FileInfo() = di.GetFiles()
            Dim dra As System.IO.FileInfo
            For Each dra In diar1
                browser.shellimages16.Images.Add(Icon.ExtractAssociatedIcon(dra.FullName))
                browser.shellimages32.Images.Add(Icon.ExtractAssociatedIcon(dra.FullName))
                browser.ListView1.Items.Add(dra.ToString, browser.shellimages32.Images.Count - 1)
            Next
        Catch ex As Exception
        End Try
        Call UpdateHeader()
        browser.ToolStripStatusLabel1.Text = browser.ListView1.Items.Count & " objects"
        browser.ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
    End Sub


As you can see, I have the code extract the icons from the files and add them to shellimages32. That works fine. But the issue is, using
Dim lvi As ListViewItem = browser.ListView1.Items.Add(IO.Path.GetFileName(dirname), imageIndex:=0)
does not use the folder icon but instead one of the file icons. What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Listview Refuses to get image

#2 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,678
  • Joined: 01-February 13

Re: Listview Refuses to get image

Posted 06 September 2015 - 10:11 AM

Did you try just using 0 for the index like this.
Dim lvi As ListViewItem = browser.ListView1.Items.Add(IO.Path.GetFileName(dirname), 0)

Was This Post Helpful? 0
  • +
  • -

#3 Pi_User5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 06-April 15

Re: Listview Refuses to get image

Posted 06 September 2015 - 10:20 AM

View PostIronRazer, on 06 September 2015 - 11:11 AM, said:

Did you try just using 0 for the index like this.
Dim lvi As ListViewItem = browser.ListView1.Items.Add(IO.Path.GetFileName(dirname), 0)

I still have the same issue as before.

I tried using this code:
Dim lvi As ListViewItem = browser.ListView1.Items.Add(IO.Path.GetFileName(dirname).ToString, browser.shellimages32.Images(browser.shellimages32.Images.Count - 1))


However, I get the error:
Error	1	Overload resolution failed because no accessible 'Add' can be called with these arguments:
    'Public Overridable Function Add(text As String, imageKey As String) As System.Windows.Forms.ListViewItem': Value of type 'System.Drawing.Image' cannot be converted to 'String'.
    'Public Overridable Function Add(text As String, imageIndex As Integer) As System.Windows.Forms.ListViewItem': Value of type 'System.Drawing.Image' cannot be converted to 'Integer'.


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6377
  • View blog
  • Posts: 25,768
  • Joined: 12-December 12

Re: Listview Refuses to get image

Posted 06 September 2015 - 10:33 AM

Have you set ListView.SmallImageList to associate your image list with the control?

(There is LargeImageList as well.)

This post has been edited by andrewsw: 06 September 2015 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 Pi_User5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 06-April 15

Re: Listview Refuses to get image

Posted 06 September 2015 - 10:37 AM

View Postandrewsw, on 06 September 2015 - 11:24 AM, said:

That isn't the same as IronRazer 's suggestion. If you read the error details you can see that the second argument to Add is just an imageKey or imageIndex, such as the number 0.

It also says "Value of type 'System.Drawing.Image' cannot be converted to 'String'", or to 'Integer'.

Please read the error details and attempt to interpret them. You don't pass the image itself as the second argument, but it's index (or key).

I know that. It doesn't matter if I use imageindex=0 or 0, the original issue remains.

View Postandrewsw, on 06 September 2015 - 11:33 AM, said:

Have you set ListView.SmallImageList to associate your image list with the control?

(There is LargeImageList as well.)

I've already done that.
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,678
  • Joined: 01-February 13

Re: Listview Refuses to get image

Posted 06 September 2015 - 10:41 AM

Well, i know i helped you with this same problem before and i believe i even told you that the 2nd parameter of the ListView.Items.Add(String, Int32) method requires an integer number of the Index of the image of the ImageList used for the ListView, not an Image as you are trying to use. If you use the other overload then the second parameter would be the Image Key you assigned to the image when you added it to the ImageList.

If using 0 for the Index as i showed does not work then, ether you do not have a Folder image as the first image in the ImageList or you have no ImageList assigned to the ListView. My guess is that it would throw an error if no ImageList was assigned to the ListView so, that leaves me to think the first image is not the one you think it is.

This post has been edited by IronRazer: 06 September 2015 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#7 Pi_User5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 06-April 15

Re: Listview Refuses to get image

Posted 06 September 2015 - 11:04 AM

I'm thinking it's because of me adding the file images to the same imagelist.

This post has been edited by andrewsw: 06 September 2015 - 11:06 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,678
  • Joined: 01-February 13

Re: Listview Refuses to get image

Posted 06 September 2015 - 11:08 AM

Now that i have looked at your code again, you are clearing all the images from both of your ImageLists just before you are going into this part to get the Directories.
        If Directory.Exists(path) Then 'Check if folder exists
            Try
                browser.ToolStripComboBox1.Text = path 'Update the address bar
                Dim treenode As New TreeNode(path)
                browser.TreeView1.Nodes.Add(treenode)
                browser.TreeView1.SelectedNode = treenode
                For Each dirname As String In IO.Directory.GetDirectories(path) 'Loop through folders in the folder
                    Dim lvi As ListViewItem = browser.ListView1.Items.Add(IO.Path.GetFileName(dirname), imageIndex:=0)
                Next
            Catch ex As Exception
            End Try
        End If



You just cleared the Images from the ImageLists so, there is no image in ether of the ImageLists to assign to the ListViewItems. You need to add the Folder Image back into both the small and large ImageLists right after you clear them so that there is an image to assign to these ListViewItems.

Or you could just iterate through the ImageLists and dispose all the images except the 1st image.
Was This Post Helpful? 1
  • +
  • -

#9 Pi_User5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 06-April 15

Re: Listview Refuses to get image

Posted 06 September 2015 - 11:14 AM

*facepalm* I knew I shouldn't have added that code in. Thank you so much!
Was This Post Helpful? 0
  • +
  • -

#10 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,678
  • Joined: 01-February 13

Re: Listview Refuses to get image

Posted 06 September 2015 - 11:17 AM

You`re Welcome. I can`t believe i did not see that the first time i looked at it ether. :whistling:
Was This Post Helpful? 0
  • +
  • -

#11 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,678
  • Joined: 01-February 13

Re: Listview Refuses to get image

Posted 06 September 2015 - 11:27 AM

Actually it is a good idea to clear the Images from the ImageLists or you may add thousands of images to them and waste memory. I am not sure how you added the folder images at the start but, you can just add them back to the ImageLists right after you clear the ImageLists.

Or instead of clearing them, you can iterate backwards through the ImageLists and just dispose all the images except the first one. Something like this.
        For i As Integer = ImageList1.Images.Count - 1 To 1 Step -1
            ImageList1.Images(i).Dispose()
            ImageList2.Images(i).Dispose()
        Next


Was This Post Helpful? 1
  • +
  • -

#12 Pi_User5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 06-April 15

Re: Listview Refuses to get image

Posted 06 September 2015 - 11:29 AM

I added the images in the editor.

This post has been edited by andrewsw: 06 September 2015 - 11:32 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6377
  • View blog
  • Posts: 25,768
  • Joined: 12-December 12

Re: Listview Refuses to get image

Posted 06 September 2015 - 11:32 AM

Note that you do not have to quote a previous post every time, there is a Reply button further down the page, or use the Fast Reply box.
Was This Post Helpful? 0
  • +
  • -

#14 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,678
  • Joined: 01-February 13

Re: Listview Refuses to get image

Posted 06 September 2015 - 11:32 AM

Then do as i showed to dispose all but the first image with a loop. Then you are starting with the 1st image of the folder in both ImageLists and just adding the file icon images to it each time.
Was This Post Helpful? 1
  • +
  • -

#15 Pi_User5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 95
  • Joined: 06-April 15

Re: Listview Refuses to get image

Posted 07 September 2015 - 06:22 PM

IronRazor: Using the code to remove all images except for the first one works! Thanks!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2