DeveloperSD
DeveloperSD

Reputation: 129

How Can I Set The Windows Form Position Manually

I am developing Desktop application, which loads a Form with different Texts and condition is so When i Click ok Button it shows Texts from the Form one by one

it is working perfectly but the Problem is i have more than one screen and say when i load a form on Current screen and click OK it stays at primary screen which is Ok ,but say when i load my form and drag it to next screen and click ok its comes back again to Primary Screen But i want it to stay on another Screen ...where i drag it into

here is line of code which loads my form

if(Form1.ShowDialog(this) == DialogResult.OK)

// at this line every time i click ok it shows the form but in Primary Screen so is there any solutions i can control the position i mean new position where i grag it into.

Upvotes: 10

Views: 21383

Answers (2)

Takepatience
Takepatience

Reputation: 316

You need to detemine the available screen first, then set the form's location.

var myScreen = Screen.FromControl(this);
var mySecondScreen= Screen.AllScreens.FirstOrDefault(s => !s.Equals(myScreen)) ?? myScreen;

form1.Left = mySecondScreen.Bounds.Left; 
form1.Top = mySecondScreen.Bounds.Top; 
form1.StartPosition = FormStartPosition.Manual; 

Upvotes: 3

Fabjan
Fabjan

Reputation: 13676

You can use Form.Location property and Form.StartPosition :

// Set the start position of the form to the manual.

form1.StartPosition = FormStartPosition.Manual;

form1.Location = new Point(100, 100);

More information :

https://msdn.microsoft.com/en-us/library/aa984420(v=vs.71).aspx

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition(VS.80).aspx

Upvotes: 12

Related Questions