PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Listbox array for Pictureboxes-VBForums
Results 1 to 22 of 22

Thread: [RESOLVED] Listbox array for Pictureboxes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Resolved [RESOLVED] Listbox array for Pictureboxes

    Hi all,

    I have 15 PBs on a form that derive their respective images from Listbox1. However it errors because the number of files (as string) in the LB varies. After doing some research I think I need to put the number of LB items in an array but I can't get it to work:

    Can anyone help?

    Code:
     
               Dim NumberOfLBItems As Integer = ListBox1.Items.Count() - 1 
               Dim Pic(NumberOfLBItems) As String
    
    
                For i = 0 To NumberOfLBItems
                    Pic(i) = ListBox1.Items(i).ToString()
                    Pic(i) = PictureBox1.ImageLocation = ListBox1.Items(i)
                Next

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Listbox array for Pictureboxes

    Are you trying to set the pb images based on what's in the list box?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Re: Listbox array for Pictureboxes

    Hi,

    Yes, exactly! Sorry I was probably vague. So if there are 7 items returned in the LB then the first 7 PBs are populated...

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Listbox array for Pictureboxes

    Something along these lines:

    Code:
    Public Class Form1
    
        Dim pb As New List(Of PictureBox)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim j As Integer
            Dim myPath As String
    
            myPath = "c:\yourPath\"
    
            ListBox1.Items.Add("re")    'the names of my images
            ListBox1.Items.Add("bl")
            ListBox1.Items.Add("ye")
    
            For j = 0 To ListBox1.Items.Count - 1
                pb.Add(New PictureBox)
                pb(j).Image = Image.FromFile(myPath & ListBox1.Items(j) & ".jpg")
                pb(j).Left = ListBox1.Right + 3
                pb(j).Top = j * 30
                pb(j).Width = 20
                pb(j).Height = 20
                pb(j).BorderStyle = BorderStyle.FixedSingle
                pb(j).SizeMode = PictureBoxSizeMode.StretchImage
                pb(j).Visible = True
                Me.Controls.Add(pb(j))
            Next
        End Sub
    The only control on my form is a listbox ("listbox1").

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Re: Listbox array for Pictureboxes

    Hi, thanks for this - excellent! Is there anyway it could accommodate a project that already has 15 PBs on it?

    (rather than creating them)

  6. #6
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Listbox array for Pictureboxes

    What doesn't make sense is the following line in your poste code
    "Pic(i) = PictureBox1.ImageLocation = ListBox1.Items(i)"
    What you trying to do with that line?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Re: Listbox array for Pictureboxes

    Hi opus,

    Ahh sorry yes. That should just read this:

    PictureBox1.ImageLocation = ListBox1.Items(i)

  8. #8
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Listbox array for Pictureboxes

    Quote Originally Posted by D_N_L View Post
    Hi, thanks for this - excellent! Is there anyway it could accommodate a project that already has 15 PBs on it?

    (rather than creating them)
    Use the code posted by vbfbryce without th lines that create and add the new picturebox!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  9. #9
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Listbox array for Pictureboxes

    What do the actual Strings in the Listbox contain? Only the Filenamens, or Filenames with path?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Re: Listbox array for Pictureboxes

    Filenames with full path...

    I tried this but no good:

    Code:
     
                Dim a As Integer
                Dim NumberOfLBItems As Integer = ListBox1.Items.Count() - 1
                Dim Pic(a) As PictureBox
    
    
                For i = 0 To NumberOfLBItems
                    For a = 1 To 15
                        Pic(a).ImageLocation = ListBox1.Items(i)
                    Next a
                Next i

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Re: Listbox array for Pictureboxes

    Correction I tried this:

    Why doesn't it work? I have 15 PBs and definitely less than or equal to 15 filepaths that can be returned to my LB.


    Code:
    Dim a As Integer
                Dim NumberOfLBItems As Integer = ListBox1.Items.Count() - 1
                Dim Pic(a) As PictureBox
    
    
                For i = 0 To NumberOfLBItems
                    For a = 1 To NumberOfLBItems
                        Pic(a).ImageLocation = ListBox1.Items(i)
                    Next a
                Next i

  12. #12
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Listbox array for Pictureboxes

    Do not declare alocal variable for the pictureboxes instead use the already created ones.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Re: Listbox array for Pictureboxes

    Thanks opus. I tried this but now I get the same image in all 15 PBs. Is that what you mean?

    Code:
     For i = 0 To NumberOfLBItems
                    PictureBox1.ImageLocation = ListBox1.Items(i)
                    PictureBox2.ImageLocation = ListBox1.Items(i)
                    PictureBox3.ImageLocation = ListBox1.Items(i)
                    PictureBox4.ImageLocation = ListBox1.Items(i)
                    PictureBox5.ImageLocation = ListBox1.Items(i)
                    PictureBox6.ImageLocation = ListBox1.Items(i)
                    PictureBox7.ImageLocation = ListBox1.Items(i)
                    PictureBox8.ImageLocation = ListBox1.Items(i)
                    PictureBox9.ImageLocation = ListBox1.Items(i)
                    PictureBox10.ImageLocation = ListBox1.Items(i)
                    PictureBox11.ImageLocation = ListBox1.Items(i)
                    PictureBox12.ImageLocation = ListBox1.Items(i)
                    PictureBox13.ImageLocation = ListBox1.Items(i)
                    PictureBox14.ImageLocation = ListBox1.Items(i)
                    PictureBox15.ImageLocation = ListBox1.Items(i)
                Next i

  14. #14
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Listbox array for Pictureboxes

    I see you have manually created those pictureboxes, so you don't have them in an array.
    Declare an empty array of pictureboxes, add each of pictureboxes to that array.
    ( Do not cŕeate new pictureboxes).
    Now you can use that Array of pictureboxes in your for-loop.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Re: Listbox array for Pictureboxes

    Hi opus,

    I tried this but am still getting the same image in each PB...


    Code:
    Dim pbs() As PictureBox
    
    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    Dim NumberOfLBItems As Integer = ListBox1.Items.Count() - 1
    
    pbs = {Me.PictureBox1, Me.PictureBox2, Me.PictureBox3, Me.PictureBox4, Me.PictureBox5, Me.PictureBox6, Me.PictureBox7, Me.PictureBox8, Me.PictureBox9, Me.PictureBox10, Me.PictureBox11, Me.PictureBox12, Me.PictureBox13, 
    Me.PictureBox14, Me.PictureBox15}   
               
               For Each p As PictureBox In pbs                
    
                    p.ImageLocation = ListBox1.Items(NumberOfLBItems)
    
                Next

  16. #16
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,911

    Re: Listbox array for Pictureboxes

    p.ImageLocation = ListBox1.Items(NumberOfLBItems)
    You're setting all pictureboxes to the same thing.

    You had code earlier that was along the correct lines, why use For Each on the destination now, rather the For on the sources as you did before?
    Code:
    For i = 0 To NumberOfLBItems
        pbs(i).ImageLocation = ListBox1.Items(i)
    Next i

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Re: Listbox array for Pictureboxes

    Hi all,

    I'm still struggling with this one. I'm sorry. To recap - I have 15 PBs already created on my form. I'm looking to 'read' their images from ListBox1. ListBox1 is working fine and returns full file paths of image locations. But I'm stuck on how to get the images into the PBs. I've reread the above this morning and come up with this:

    Alas, it doesn't work...

    I thought it may be because my PBs start at 1 whereas the LB index begins at 0. But I don't think it is.

    Am I doing something fundamentally wrong here?

    Code:
    Dim pbs As Array
            pbs = {Me.PictureBox1, Me.PictureBox2, Me.PictureBox3, Me.PictureBox4, Me.PictureBox5, _
                   Me.PictureBox6, Me.PictureBox7, Me.PictureBox8, Me.PictureBox9, Me.PictureBox10, _
                   Me.PictureBox11, Me.PictureBox12, Me.PictureBox13, Me.PictureBox14, Me.PictureBox15}
    
            Dim i As Integer
    
            Dim NumberOfLBItems As Integer = ListBox1.Items.Count() - 1
    
            For i = 0 To NumberOfLBItems
                pbs(i).ImageLocation = ListBox1.Items(i)
            Next I

  18. #18
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Listbox array for Pictureboxes

    I have a project with only one listbox and one picturebox on the form. This sets the image properly:

    Code:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim pb As PictureBox
            Dim strLB As String
    
            pb = PictureBox1
            strLB = ListBox1.Items(0).ToString  'only 1 item in listbox, ie. "c:\users...picture.jpg"
            pb.ImageLocation = strLB
        End Sub

  19. #19
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Listbox array for Pictureboxes

    Now for an array, 2 picture boxes:

    Code:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim pb(1) As PictureBox
            Dim strLB As String
    
            pb(0) = PictureBox1
            strLB = ListBox1.Items(0).ToString  '2 items in listbox
            pb(0).ImageLocation = strLB
            pb(1) = PictureBox2
            strLB = ListBox1.Items(1).ToString
            pb(1).ImageLocation = strLB
        End Sub

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Re: Listbox array for Pictureboxes

    Hi vbfbryce,

    Thanks! That has given me some good direction. : )

    Only thing is it bugs out if the declared array is greater than the number of images to return. So if my array is (15) but only 6 images will be returned I receive the Argument our of range error for PB7:

    InvalidArgument=Value of '6' is not valid for 'index'. Parameter name: index

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Mar 2014
    Posts
    149

    Re: Listbox array for Pictureboxes

    Thank you kindly to everyone that has helped.

    Figured it out:

    Code:
    Dim pbs As Array
            pbs = {Me.PictureBox1, Me.PictureBox2, Me.PictureBox3, Me.PictureBox4, Me.PictureBox5, _
                    Me.PictureBox6, Me.PictureBox7, Me.PictureBox8, Me.PictureBox9, Me.PictureBox10, _
                    Me.PictureBox11, Me.PictureBox12, Me.PictureBox13, Me.PictureBox14, Me.PictureBox15}
    
            Dim strLB As String
    
            Dim NumberOfLBItems As Integer = ListBox1.Items.Count() - 1
    
            For x = 0 To NumberOfLBItems
                strLB = ListBox1.Items(x).ToString
                pbs(x).ImageLocation = strLB
            Next

  22. #22
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,230

    Re: [RESOLVED] Listbox array for Pictureboxes

    Hi,

    I see that you have a solution which is great but just for information there is an easier way to do this. If you name your Static PictureBox's correctly then you can access the Forms Controls collection to specify the individial PictureBox that you want to set.

    So, if your Static PictureBox's where called PicBox0, PicBox1, PicBox2 etc, etc.. then you could say:-

    vb.net Code:
    1. For Counter As Integer = 0 To ListBox1.Items.Count - 1
    2.   DirectCast(Me.Controls(String.Format("PicBox{0}", Counter)), PictureBox).ImageLocation = ListBox1.Items(Counter).ToString
    3. Next

    Hope that helps.

    Cheers,

    Ian

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width