Page 1 of 1

Random Location within Form's Borders?

Posted: Thu Sep 15, 2011 1:00 am
by zachman61
Well im making a game and i need the picturebox when it is clicked form the original location it will move to a new random location and should hardly ever give the same location or a location near it too often.

Can anyone tell me how this can be done?

Re: Random Location within Form's Borders?

Posted: Thu Sep 15, 2011 3:20 am
by CodenStuff
Something like this maybe:
Code: Select all
 Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click
        Dim R As New Random
        PictureBox1.Location = New Point(R.Next(0, Me.Width - PictureBox1.Width), R.Next(0, Me.Height - PictureBox1.Height))
    End Sub

Re: Random Location within Form's Borders?

Posted: Thu Sep 15, 2011 9:32 pm
by zachman61
I'll try it out and post the results :D

Re: Random Location within Form's Borders?

Posted: Thu Sep 15, 2011 9:53 pm
by Zulf
Code: Select all
public void getRandom()
{
    Random rand = new Random();
    int width = rand.Next(0, this.Width);
    int height = rand.Next(0, this.Height);
}