Results 1 to 7 of 7

Thread: Random Picture Box Generation

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    2

    Random Picture Box Generation

    Hey guise, newbie here.

    I would like a little help with some implementation in my game. I'm a little confused as to how I would make picture boxes generate randomly on the form with random images within them. Any help would be significantly appreciated.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: Random Picture Box Generation

    What I'd do is not generate random pictureboxes, rather generate random images in a static picturebox. Simply add a picturebox and a button to your form, and then open up the code view. In the code view declare a new list(of images) and a new instance of the random class at the form level. In your form_load event load the desired images to the list using the .Add or the .AddRange method. Next, in the button's click event you'd set the picturebox's image equal to a random item in your list(of images) using the Random.Next:
    Code:
    PictureBox1.Image = fooList.Item(r.Next(0, fooList.Count + 1))
    Last edited by dday9; May 7th, 2013 at 09:13 AM. Reason: MSDN Links
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Random Picture Box Generation

    Quote Originally Posted by dday9 View Post
    ...
    Code:
    PictureBox1.Image = fooList.Item(r.Next(0, fooList.Count + 1))
    I think that you need to change fooList.Count + 1 to fooList.Count.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Random Picture Box Generation

    Yeah, you do. Otherwise, that's what I'd do, too. I don't like creating new controls unless I absolutely have to. Create them once, cache them if needed, then toggle their visibility. Dynamic creation will work, but you may well end up fighting performance issues if you do that.
    My usual boring signature: Nothing

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: Random Picture Box Generation

    Quote Originally Posted by dbasnett View Post
    I think that you need to change fooList.Count + 1 to fooList.Count.
    Yeah I 'free-typed' it, so I didn't get a chance to debug it. I was just thinking "oh exclusive u-bound, just add 1", but forgot about the 0 based index.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Random Picture Box Generation

    Quote Originally Posted by dday9 View Post
    Yeah I 'free-typed' it, so I didn't get a chance to debug it. I was just thinking "oh exclusive u-bound, just add 1", but forgot about the 0 based index.
    I knew you knew, just correcting it in case the OP tried it before you realized.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    2

    Re: Random Picture Box Generation

    Quote Originally Posted by dday9 View Post
    What I'd do is not generate random pictureboxes, rather generate random images in a static picturebox. Simply add a picturebox and a button to your form, and then open up the code view. In the code view declare a new list(of images) and a new instance of the random class at the form level. In your form_load event load the desired images to the list using the .Add or the .AddRange method. Next, in the button's click event you'd set the picturebox's image equal to a random item in your list(of images) using the Random.Next:
    Code:
    PictureBox1.Image = fooList.Item(r.Next(0, fooList.Count + 1))
    Thank you! So this is how I can get random images into my picture boxes. I am planning to have multiple, random picture boxes move in from the sides of the form with random images within each of them. So that's sorted.

    Quote Originally Posted by Shaggy Hiker View Post
    Yeah, you do. Otherwise, that's what I'd do, too. I don't like creating new controls unless I absolutely have to. Create them once, cache them if needed, then toggle their visibility. Dynamic creation will work, but you may well end up fighting performance issues if you do that.
    Yeah, I want to try and avoid any performance issues.

    My next step is to randomly assign movement to these picture boxes so that only a select few are in movement.

    Cheers for the help guys!

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