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. :)
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))
Re: Random Picture Box Generation
Quote:
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.
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.
Re: Random Picture Box Generation
Quote:
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.
Re: Random Picture Box Generation
Quote:
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.
Re: Random Picture Box Generation
Quote:
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.
Quote:
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!