Tutorials Navigation
[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:
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
Comments
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(3,500)
- 02. How to: Matrix Numbers | Batch File(1,908)
- 03. How to Password Protect Files on Windows(857)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,736)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,146)
- 06. How to embed an image on TheTechGame(3,100)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(12,993)
- 08. Host bot lobbies! Full Tutorial!(11,292)
- 09. Unban yourself [Plutonium BO2](14,243)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,390)
- 11. Best Crosshair Settings for Valorant(6,529)
- 12. Othercide The Surgeon Boss Guide(2,544)
- 13. Othercide Remembrances Unlock Guide(4,470)
- 14. Othercide Beginners Tips and Tricks(2,712)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,848)
"[C#] Change location of Control on Form programatically" :: Login/Create an Account :: 1 comment