Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,410,182

[C#] Change location of Control on Form programatically

Tutorial Name: [C#] Change location of Control on Form programatically  

Category: PC Tutorials

Submitted By: Skittle

Date Added:

Comments: 1

Views: 6,290

Related Forum: PC Building Forum

Share:

This tutorial links in with my Random Number Tutorial, check it out if you need help with Random Numbers in this tutorial!
This tutorial will show all of you how to move a Control on a Windows Form, for you guys, I will be moving a PictureBox with a cat
First off, create your project and setup the Control you are going to move and a button. Here is my layout:
[ Register or Signin to view external links. ]

First, you will need to handle the click event of the button we have. Simply double click the button and proceed to our code. The Data Type we will primarily be using is called the Point, it is a set of two integers that act as X and Y co-ordinates for the position of the top-left corner of Controls relative to the Form. I have set two variables that hold the minimum and maximum co-ordinates for out Cat picture to be, here are the variables:

Point min = new Point(12, 12);
Point max = new Point(472, 450);


NOTE: My form is 650x650 pixels so the margins will not always be the same for different form sizes.
The position (12, 12) is the top-left margin of the form, so that is the furthest left and up that the picture can go.
[ Register or Signin to view external links. ]
The position (472, 450) is the bottom-right margin of the form, so that is the furthest right and down that the picture can go.
[ Register or Signin to view external links. ]

So the first parameter of Point is the X value, and the second one is the Y value.
Now we will need a random number generator so the position changes each time we click the button, let's create a Random variable just like in my Random Number Tutorial:

Random rnd = new Random();

Now we will need a variable to hold the final position we will set to our Cat in the end:

Point finalLocation; //Point variable to hold final location for Cat


So now that we have all of our variables, we can generate the random position of out Cat! Here is how we do this:

/*
min.X = 12
min.Y = 12
max.X = 472
max.Y = 450
*/
           
finalLocation.X = rnd.Next(min.X, max.X); //generating random X co-ordinate
//the X location will be between 12 and 472
finalLocation.Y = rnd.Next(min.Y, max.Y); //generating random Y co-ordinate
//the Y location will be between 12 and 450

Finally, we need to set the random location to out Cat picture, here is the code for this:

Cat.Location = finalLocation; //setting the Location of the Cat to the variable storing the random position

If you found this tutorial helpful, any Rep is greatly appreciated
If you need any help, feel free to PM me

Ratings

Current rating: 4.29 by 7 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"[C#] Change location of Control on Form programatically" :: Login/Create an Account :: 1 comment

If you would like to post a comment please signin to your account or register for an account.

djPosted:

Noice share skittle. Glad to see you getting more into coding bud.