ImageList read from directory and populate ListView code problem

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4321 Views - Last Post: 09 December 2013 - 04:01 PM Rate Topic: -----

#1 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

ImageList read from directory and populate ListView code problem

Posted 05 December 2013 - 11:44 AM

OK so i am new here and i am getting down to business, so please dont hold it against me because its my first post. I did however take the time to read the rules. so here it goes

i am trying to populate a imagelist from a directory but i think i am doing something wrong, i dont get an error but the images dont show up in my listview. I did bind the LargeImageList to the imagelist control, but it doesn't show any images in listview. if anyone can tell me what i am doing wrong, that be great
Dim DocDir As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        Dim ScreenshotFolder As String = ("\images\")
        Dim DoubleDir As String = DocDir & ScreenshotFolder
        Dim FullPath As String = DoubleDir & NameLabel.Text ' NameLabel.Text holds a folder name
        Dim Counter As Integer = 0
        Try
            Dim Di As DirectoryInfo = New DirectoryInfo(FullPath)
            Dim Files As FileInfo() = Di.GetFiles("*.jpg")
            For Each Fi In Di.GetFiles()
                Me.ImageListBig.Images.Add(Image.FromFile(Fi.Name)) 'ImageListBig is my imagelist
                If (Counter < 60) Then
                    Counter = Counter + 1
                End If
            Next

        Catch ImageLoad As Exception
            MsgBox(ImageLoad.Message)
        End Try


Is This A Good Question/Topic? 0
  • +

Replies To: ImageList read from directory and populate ListView code problem

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,311
  • Joined: 12-December 12

Re: ImageList read from directory and populate ListView code problem

Posted 05 December 2013 - 12:36 PM

If no error occurs then it suggests to me that it is not looking in the correct location, so the For Each Fi is never entered.

Use a MessageBox to display the FullPath value, and another MessageBox in the For Each to see if it is being entered at all.

There is a debugging tutorial linked in my signature which would enable you to step through your code.

This post has been edited by andrewsw: 05 December 2013 - 12:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1538
  • View blog
  • Posts: 3,869
  • Joined: 01-February 13

Re: ImageList read from directory and populate ListView code problem

Posted 05 December 2013 - 07:37 PM

Hi,
Maybe you have other code in your program to add the images to a ListViewItem in the ListView but, if not then you have to add an item to the ListView and set the image to use with the item from the ImageList.

Also i wanted to mention that you should use the Path Class when combining paths and doing other tasks involving file or directory paths.

Here is an example that i have commented to help you understand it. I added 1 listview and 1 imagelist named the same as yours and set the listview`s LargeImageList to the ImageList. I also set the imagelist size property to (100, 70).
Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Combine the MyDocuments directory path with the directory name "Images"
        Dim MyPicPath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Images")

        'Combine MyPicPath directory with the directory name in the label.
        Dim FullPath As String = Path.Combine(MyPicPath, NameLabel.Text)

        'Loop threw all Jpg image file names in the FullPath
        For Each ImagePathName As String In Directory.GetFiles(FullPath, "*.jpg")

            'Add a new bitmap image to the ImageList
            ImageListBig.Images.Add(New Bitmap(ImagePathName))

            'Add a new Item to the listview.
            'Here i set the Items Text to the filename and set the image index to the last image index added to the ImageList
            ListView1.Items.Add(Path.GetFileName(ImagePathName), ImageListBig.Images.Count - 1)
        Next

    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#4 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: ImageList read from directory and populate ListView code problem

Posted 06 December 2013 - 06:24 AM

I really do not get this. I get:

Posted Image

Then it returns this:

Posted Image

Which is a file in the directory where the images are. So if the code is working, what else could be wrong with it?

I dont suppose anyone here has similar code that i could use to see if i can get it working. It would also help me compare my code and see if i can find where i went wrong.
Was This Post Helpful? 0
  • +
  • -

#5 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: ImageList read from directory and populate ListView code problem

Posted 06 December 2013 - 08:41 AM

Sorry guys, i only seen your replies now. Razor i tried your code and I kinda got it doing something, as it seems to be populating, except i don't see any of the content. I have no clue what could be causing this. Any ideas? Screenshot below of the listview

Posted Image
Was This Post Helpful? 0
  • +
  • -

#6 HeBTeMy   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: ImageList read from directory and populate ListView code problem

Posted 06 December 2013 - 10:06 AM

Why don't you debug and press F10 with a breakpoint and see what's really happening?
By the way, is that really your listview? :D
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,311
  • Joined: 12-December 12

Re: ImageList read from directory and populate ListView code problem

Posted 06 December 2013 - 10:50 AM

That's an informative screenshot :online2long:
Was This Post Helpful? 0
  • +
  • -

#8 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: ImageList read from directory and populate ListView code problem

Posted 07 December 2013 - 06:31 AM

View Postandrewsw, on 06 December 2013 - 10:50 AM, said:

That's an informative screenshot :online2long:/>

Thanks Iron, your code helped, but i don't know what its doing populating empty spaces.... im officially lost

lol Andrew, what you want me to share? that is what i see Andrew. It seems to populate according to the scroll bars, but i don't see any images at all. So my next question would be why?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,311
  • Joined: 12-December 12

Re: ImageList read from directory and populate ListView code problem

Posted 07 December 2013 - 06:58 AM

Disable your Try..Catch block so that you can see the full error details. You will also be able to press the Debug, or Break, button that appears and it will take you to the line that generated the error.

Try..Catch blocks are obviously important, but you need to see as much information as possible, and be able to debug the code, during development.

There is a debugging tutorial linked in my signature.

Sheepings said:

So if the code is working, ..

The code isn't working, that is why you see the error message in a MessageBox.

This post has been edited by andrewsw: 07 December 2013 - 06:56 AM

Was This Post Helpful? 0
  • +
  • -

#10 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: ImageList read from directory and populate ListView code problem

Posted 07 December 2013 - 07:36 AM

View PostSheepings, on 06 December 2013 - 02:24 PM, said:

I really do not get this. I get:

Posted Image

Then it returns this:

Posted Image

Which is a file in the directory where the images are. So if the code is working, what else could be wrong with it?

I dont suppose anyone here has similar code that i could use to see if i can get it working. It would also help me compare my code and see if i can find where i went wrong.


At what line do you get "Object reference not set to an instance of an object."?

Here are some common "Object reference not set to an instance of an object." errors and how to fix them

This post has been edited by Michael26: 07 December 2013 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#11 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1538
  • View blog
  • Posts: 3,869
  • Joined: 01-February 13

Re: ImageList read from directory and populate ListView code problem

Posted 07 December 2013 - 12:04 PM

Quote

Thanks Iron, your code helped, but i don't know what its doing populating empty spaces.... im officially lost



The first thing i have to ask is what is the View property of the ListView set to (Details, List, Tile, LargeIcon, or SmallIcon)? If it is set to LargeIcon or Tile and the LargeImageList property of the ListView is set to the ImageList (ImageListBig) then the code i provided should work fine.

If you have the View property set to SmallIcon or List then set the ListViews SmallImageList property to the ImageList (ImageListBig).

If you have the View property set to Details then you need to set the SmallImageList property of the ListView to the ImageList (ImageListBig) and you will also need to add at least 1 Column to the listview. You can do that in the ListView`s Columns property.

This post has been edited by IronRazer: 07 December 2013 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#12 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: ImageList read from directory and populate ListView code problem

Posted 08 December 2013 - 06:04 AM

Thanks for the help guys but i fixed the error, but now i am faced with a new one. Has anyone here got an idea why the listview populates but it populates and doesn't show any images. BUT, if i hover my mouse over empty space, i get a "balloon tip" which shows me the filename but i can not see any images. What am i doing wrong here?
Was This Post Helpful? 0
  • +
  • -

#13 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1538
  • View blog
  • Posts: 3,869
  • Joined: 01-February 13

Re: ImageList read from directory and populate ListView code problem

Posted 08 December 2013 - 06:58 PM

View PostSheepings, on 08 December 2013 - 08:04 AM, said:

Thanks for the help guys but i fixed the error, but now i am faced with a new one. Has anyone here got an idea why the listview populates but it populates and doesn't show any images. BUT, if i hover my mouse over empty space, i get a "balloon tip" which shows me the filename but i can not see any images. What am i doing wrong here?

What problem did you fix? Did you read my last post? Do you have the correct imagelist property set for the View property you have the listview set to use? If you don`t it will set the text but, the image will be nothing which means you will get the item added to the listview with the text set but, the image won`t be shown.

If this does not help then i would check to see if the images are not "Nothing" when they are being loaded from the file and added to the ImageListBig. That can be done by setting a break point in the code and debugging it. andrewsw has a debugging tutorial here on DIC that you can check out if you need to.

This post has been edited by IronRazer: 08 December 2013 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

#14 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: ImageList read from directory and populate ListView code problem

Posted 09 December 2013 - 03:04 PM

View PostIronRazer, on 08 December 2013 - 06:58 PM, said:

What problem did you fix? Did you read my last post? Do you have the correct imagelist property set for the View property you have the listview set to use? If you don`t it will set the text but, the image will be nothing which means you will get the item added to the listview with the text set but, the image won`t be shown.

If this does not help then i would check to see if the images are not "Nothing" when they are being loaded from the file and added to the ImageListBig. That can be done by setting a break point in the code and debugging it. andrewsw has a debugging tutorial here on DIC that you can check out if you need to.

1. I fixed the object/instance error.
2. Yes i have read it.
3. Yes I am almost sure i do have the right settings in the properties... and if you mean "enabled" where you said "set to use", then ye, otherwise i don't know what you are asking me.
4. The text is not even set, nor is the images. I have to hover my mouse over where i would expect there to be an item and i get a tooltip displaying imported files, but I cant see any images or text VISIBLY.
5. Right now there is no error, It simply doesn't work or display any images (But it is populating invisibly). lol Weird right?

Posted Image
That's what i get when i hover mouse ^
Was This Post Helpful? 0
  • +
  • -

#15 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: ImageList read from directory and populate ListView code problem

Posted 09 December 2013 - 03:23 PM

" It simply doesn't work " doesn't tell us much, if that happens then you have to turn to debugging.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2