|
-
May 7th, 2013, 06:18 AM
#1
Thread Starter
New Member
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.
-
May 7th, 2013, 09:12 AM
#2
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
-
May 7th, 2013, 09:19 AM
#3
Re: Random Picture Box Generation
 Originally Posted by dday9
...
Code:
PictureBox1.Image = fooList.Item(r.Next(0, fooList.Count + 1))
I think that you need to change fooList.Count + 1 to fooList.Count.
-
May 7th, 2013, 09:23 AM
#4
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
 
-
May 7th, 2013, 09:25 AM
#5
Re: Random Picture Box Generation
 Originally Posted by dbasnett
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.
-
May 7th, 2013, 09:32 AM
#6
Re: Random Picture Box Generation
 Originally Posted by dday9
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.
-
May 8th, 2013, 04:29 AM
#7
Thread Starter
New Member
Re: Random Picture Box Generation
 Originally Posted by dday9
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.
 Originally Posted by Shaggy Hiker
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|