Results 1 to 11 of 11

Thread: [RESOLVED] Adding a picture to pic box from a list box.

  1. #1

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Resolved [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:
    1. Dim fso As New Scripting.FileSystemObject
    2. Dim xFolder As Folder
    3. Dim xFile As File
    4.  
    5. Set xFolder = fso.GetFolder("C:\Pictures")
    6. For Each xFile In xFolder.Files
    7.     List1.AddItem xFile.Name
    8. 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:
    1. Dim I As Long
    2. Dim xStr As String
    3.  
    4. For I = List1.ListCount - 1 To 0 Step -1
    5.     If List1.Selected(I) Then
    6.     Open "C:\Pictures" & Form1.List1.List(I) For Binary As #1
    7.         xStr = Space(LOF(1))
    8.         Get #1, , xStr
    9.         Picture1.Picture = xStr
    10.         Close #1
    11.         Exit For
    12.    
    13. End If
    14. 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
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Adding a picture to pic box from a list box.

    Use LoadPicture() function:
    VB Code:
    1. Private Sub List1_Click()
    2.     Picture.Picture = LoadPicture(List1.List(List1.ListIndex))
    3. 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:
    1. Private Sub List1_Click()
    2. Dim strImagePath$
    3.  
    4.     strImagePath = "C:\Pictures\" & List1.List(List1.ListIndex)
    5.     Picture.Picture = LoadPicture(strImagePath)
    6. End Sub
    Last edited by RhinoBull; Jul 6th, 2005 at 08:26 PM.

  3. #3

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Adding a picture to pic box from a list box.

    aaaHaaa! Thats what I needed! Thank you for your help once again!

    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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:
    1. Private Sub List1_Click()
    2.  
    3.      Dim strImagePath As String
    4.  
    5.      strImagePath = "C:\Pictures\" & List1.List(List1.ListIndex)
    6.  
    7.      [b]If Dir(strImagePath) <> "" Or Right(strImagePath ,4) = ".bmp" Or Right(strImagePath,4) = ".jpeg" Then[/b]
    8.  
    9.           Picture.Picture = LoadPicture(strImagePath)
    10.  
    11.      [b]Else
    12.  
    13.           MsgBox "Invalid filepath or image", vbCritical
    14.  
    15.      End If[/b]
    16.  
    17. End Sub

  5. #5

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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:
    1. Dim i as long
    2. Picture1.Picture = LoadPicture("C:\Pictures\" & List1.List(i))

    And I will intertwine the code you gave Jacob.

    Thank you again!
    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  6. #6

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  7. #7
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: [RESOLVED] Adding a picture to pic box from a list box.

    Sorry, heres the change that was made:

    VB Code:
    1. 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

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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"

  9. #9

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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 :P

    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

  10. #10
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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.

  11. #11

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: [RESOLVED] Adding a picture to pic box from a list box.

    Yes, it opens .gif files also! Thank you once again!
    Stilekid007
    Quote Originally Posted by stilekid007
    I RATE ALL HELPFULL POSTS!

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