none
Fill multiple picture boxes from an image list using a for i loop RRS feed

  • Question

  • Hi all,

    I'm making a blackjack game and want to set the 10 picture boxes to face-down cards (5 for dealer, 5 for player) how can I loop through the picture boxes?? each one has the same image put in it. They are named pbxdealer_1, pbxdealer_2 etc... and pbxplayer_1, pbxplayer_2 etc...

    Here is what I came up with:

    Dim strdealer As String = "pbxdealer_"
            For i As String = 1 To 5
                Dim fill_box As PictureBox = (strdealer)(i)
                 (fill_box) as picturebox .image = imgcards.Images(52)
            Next

    As you can see, I have been messing around with data types to try and make it understand :P

    Any help is appreciated, thanks in advance :)

    Wednesday, June 13, 2012 10:58 AM

Answers

  • Ah, well then I would suggest you to create a method like
     List<PictureBox> picBoxes = new List<PictureBox>();
    
                picBoxes.Add(pbxdealer_1);
                picBoxes.Add(pbxdealer_2);
                picBoxes.Add(pbxplayer_1);
                picBoxes.Add(pbxplayer_2);
    
                foreach (var pictureBox in picBoxes)
                {
                    pictureBox.Image = imgcards.Images(52);
                }
    The code is for C#, you might need to convert it to vb.net

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    • Marked as answer by jmonkey610 Thursday, June 14, 2012 10:48 AM
    Thursday, June 14, 2012 10:35 AM

All replies

  • make a list of it and then you can enumerate through all the images.

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, June 13, 2012 11:14 AM
  • How do you mean?? I have not had much experience with VB and have been messing around with enumerator for the last 40 mins however it always returns an error along the lines of "cannot convert string to picturebox type"

    Could you post some syntax please??

    Wednesday, June 13, 2012 12:29 PM
  • in string you would be having path for an image. and picturebox.Image expects image, so for that you need to convert a string to an image.

    Check this link


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Thursday, June 14, 2012 8:03 AM
  • I dont think I explained the issue very well:

    I currently have this:

                pbxdealer_1.Image = imgcards.Images(52)
                pbxdealer_2.Image = imgcards.Images(52)
                pbxdealer_3.Image = imgcards.Images(52)
                pbxdealer_4.Image = imgcards.Images(52)
                pbxdealer_5.Image = imgcards.Images(52)
    
                pbxplayer_1.Image = imgcards.Images(52)
                pbxplayer_2.Image = imgcards.Images(52)
                pbxplayer_3.Image = imgcards.Images(52)
                pbxplayer_4.Image = imgcards.Images(52)
                pbxplayer_5.Image = imgcards.Images(52)

    However, as you can see its a lot of code to do a very similar task.

    I want to be able to loop the last number of the picture-box so it is more clean and so I can setup a function to set certain boxes to cards by number rather than repeating the above code throughout my project.

    Thursday, June 14, 2012 10:28 AM
  • Ah, well then I would suggest you to create a method like
     List<PictureBox> picBoxes = new List<PictureBox>();
    
                picBoxes.Add(pbxdealer_1);
                picBoxes.Add(pbxdealer_2);
                picBoxes.Add(pbxplayer_1);
                picBoxes.Add(pbxplayer_2);
    
                foreach (var pictureBox in picBoxes)
                {
                    pictureBox.Image = imgcards.Images(52);
                }
    The code is for C#, you might need to convert it to vb.net

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    • Marked as answer by jmonkey610 Thursday, June 14, 2012 10:48 AM
    Thursday, June 14, 2012 10:35 AM
  • Thank you so much!! This is exactly what I needed :)
    Thursday, June 14, 2012 10:48 AM