Random Location within Form's Borders?

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
4 posts Page 1 of 1
Contributors
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

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?
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

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
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

I'll try it out and post the results :D
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
Zulf
Serious Programmer
Serious Programmer
Posts: 441
Joined: Fri Jun 11, 2010 7:46 am

Code: Select all
public void getRandom()
{
    Random rand = new Random();
    int width = rand.Next(0, this.Width);
    int height = rand.Next(0, this.Height);
}
Image
4 posts Page 1 of 1
Return to “Tutorial Requests”