[RESOLVED] Adding a picture to pic box from a list box.
Hello,
I have a picture box and a list box on my form.
I am using this code to put the names of all the files in a list box.
VB Code:
Dim fso As New Scripting.FileSystemObject
Dim xFolder As Folder
Dim xFile As File
Set xFolder = fso.GetFolder("C:\Pictures")
For Each xFile In xFolder.Files
List1.AddItem xFile.Name
Next
Now what I need to do is get it to get the "checkboxed" item in the list and open that file in the pic box.
I have this code below:
VB Code:
Dim I As Long
Dim xStr As String
For I = List1.ListCount - 1 To 0 Step -1
If List1.Selected(I) Then
Open "C:\Pictures" & Form1.List1.List(I) For Binary As #1
xStr = Space(LOF(1))
Get #1, , xStr
Picture1.Picture = xStr
Close #1
Exit For
End If
Next
But I get Object required error on xStr in this line " Picture1.Picture = xStr"
Does anyone know how I can fix this?
Thank you!
Stilekid007
Re: Adding a picture to pic box from a list box.
Use LoadPicture() function:
VB Code:
Private Sub List1_Click()
Picture.Picture = LoadPicture(List1.List(List1.ListIndex))
End Sub
NOTE: List1.List(List1.ListIndex) must be valid image path.
EDIT:
Image path may look like the following:
"C:\Pictures\" & List1.List(List1.ListIndex) so you'd use it with LoadPicture():
VB Code:
Private Sub List1_Click()
Dim strImagePath$
strImagePath = "C:\Pictures\" & List1.List(List1.ListIndex)
Picture.Picture = LoadPicture(strImagePath)
End Sub
Re: Adding a picture to pic box from a list box.
aaaHaaa! Thats what I needed! Thank you for your help once again! :thumb:
Stilekid007
Re: [RESOLVED] Adding a picture to pic box from a list box.
Or you can try this to check if the picture even exists, or is a valid picture file that the picturebox can support:
VB Code:
Private Sub List1_Click()
Dim strImagePath As String
strImagePath = "C:\Pictures\" & List1.List(List1.ListIndex)
[b]If Dir(strImagePath) <> "" Or Right(strImagePath ,4) = ".bmp" Or Right(strImagePath,4) = ".jpeg" Then[/b]
Picture.Picture = LoadPicture(strImagePath)
[b]Else
MsgBox "Invalid filepath or image", vbCritical
End If[/b]
End Sub
Re: [RESOLVED] Adding a picture to pic box from a list box.
Thank you to both of you for the responses.
Here is what I used. When the list is loaded it only loads the filenames of the files that are in the folder.
So I used this code to open that file"
VB Code:
Dim i as long
Picture1.Picture = LoadPicture("C:\Pictures\" & List1.List(i))
And I will intertwine the code you gave Jacob.
Thank you again!
Stilekid007
Re: [RESOLVED] Adding a picture to pic box from a list box.
Hello Jacob!
Well I tried your code. It works great BUT if I try to open a txt file that is on the list it gives me the VB ERROR Invalid image instead of giving me the "MsgBox "Invalid filepath or image", vbCritical"
Do you happen to know why?
Thank you!
Stilekid007
Re: [RESOLVED] Adding a picture to pic box from a list box.
Sorry, heres the change that was made:
VB Code:
If Dir(strImagePath) <> "" And (Right(strImagePath ,4) = ".bmp" Or Right(strImagePath,4) = ".jpeg") Then
Pretty hard when using the top of your head instead of VB :bigyello:
Re: [RESOLVED] Adding a picture to pic box from a list box.
The following will NEVER be true:
Right(strImagePath, 4) = ".jpeg"
You should have
Right(strImagePath, 4) = ".jpg"
OR
Right(strImagePath, 5) = ".jpeg"
Re: [RESOLVED] Adding a picture to pic box from a list box.
Ahh yes, now that works like a beauty! Thank you guys you saved the day! :P :thumb: :thumb: :thumb: :thumb: :thumb: :P
Stilekid007
Re: [RESOLVED] Adding a picture to pic box from a list box.
Quote:
Originally Posted by RhinoBull
The following will NEVER be true:
Right(strImagePath, 4) = ".jpeg"
You should have
Right(strImagePath, 4) = ".jpg"
OR
Right(strImagePath, 5) = ".jpeg"
Thanks for clearing that up, Rhinobull. I ment to type in .jpg, not .jpeg in that example. But he can add Right(strImagePath, 5) = ".jpeg" to support that also. I believe Pictureboxes can open Gifs as well. :)
Re: [RESOLVED] Adding a picture to pic box from a list box.
Yes, it opens .gif files also! Thank you once again!
Stilekid007