If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

This has to be easy, but I can't figure out how to make those pictureboxes in flowlayoutpanel clickable. Any example will do - opening it in default picture browser, using it as panel background or using it as a picture for separate picturebox - anything will do - I simply do not know how to get the file name assiciated with the particular picturebox in the flowlayoutpanel.

Re: Clickable picture box in flowlayoutpanel

Funny, I was actually going to suggest in your other thread that you use a UserControl that consisted of a PictureBox + Label, and then a FlowLayoutPanel which you add them to. Seems you've done similar.

Re: Clickable picture box in flowlayoutpanel

Thanks for interest ForumAccount – I appreciate that. Google was first on this occasion , but maybe you (or anybody else) will be able to help me with the above code.

Everything so far works as expected, but(!) if the folder contains files other than images it returns an 'Out of Memory' exception. Is there a way to modify the above code so it will look for specified extensions only (jpg, bmp, png, gif etc) and ignore any other files? It stops processing further files if the error occurs (typically it goes in alphabetical order).

lets say I have such files in folder:
aaa.jpg
bbb.gif
ccc.zip
ddd.jpg

Then it will add only aaa.jpg and bbb.gif to the flowlayoutpanel, throw an exception at ccc.zip and never process ddd.jpg.

Re: Clickable picture box in flowlayoutpanel

Inside your loop you are getting every file that is in the directory. You aren't actually checking whether:

The valid has a valid image extension

The file is a valid image

What you need to do is, inside your loop add some logic that filters out the files you don't want. Then use a Try...Catch block to catch any exceptions from trying to read the file. When you are coding, you should be trying to see what exceptions each method can throw and determining how you can deal with them. For the Image.FromFile method, for example, you see that it can throw 3 different exceptions. You will also see that one of those exceptions is an OutOfMemoryException that occurs when:

The file does not have a valid image format.
-or-
GDI+ does not support the pixel format of the file.

You will have to use a Try...Catch block because there is no code that you can use that can safely determine whether a file is a valid image without it throwing an exception.

Re: Clickable picture box in flowlayoutpanel

Hi ForumAccount,

Of course I catched the exception. It simply says Out of Memory - this is it.

I tested it by moving files in the folder around and I came to the conclusion that the error is cause by "file which does not have a valid image format". I put a TXT file into the folder - exception. I remove the TXT from the folder - no exception. Same with other extensions which aren't representing graphics.

Re: Clickable picture box in flowlayoutpanel

You would have been better asking this as a new question, you might get confused. You actually have the right idea. I would suggest a change to the directoryinfo class instead of just directory. Like this:

Code:

Dim DirInfo As New IO.DirectoryInfo(fbd.SelectedPath)

Now when you ask for the files, you can get FileInfo back instead of just filename.

Code:

For Each F As FileInfo In DirInfo.GetFiles()

You can then access stuff like F.Extension

Code:

Select Case F.Extension.ToLower
Case ".jpg", ".bmp", ".png"
Try 'Notice the try INSIDE the loop, as you do not want to fail at the first problem, but ignore and keep going
Dim NewImage As Image = Image.FromFile(F.FullName)
....
Catch ex As Exception
End Try
End Select