Reputation: 26929
From main form (Form1) I am calling to show another form(Form2). but I want it to show exactly the same place and size that form1 is, so that we wont be able to see form1 anymore until we either close form2 or move it somewhere else. so I wrote these lines:
Form2 f2 = new Form2();
f2.Left = this.Left;
f2.Top = this.Top;
f2.Size = this.Size;
f2.Show();
But it still has problems. form2 us not completely on form1. any other thing I should add to the code?
Upvotes: 4
Views: 8287
Reputation: 1
In your form properties for form2, set it to "center on parent"
If the forms are the same size, then this will place form2 over form1 and form 1 is not accessible. Still open the form using modal (form2.ShowDialog()) so focus stays with form2 even if the user moves form two manually.
You will still be able to move form2 like I just mentioned off of form one, but that was not specified as part of this question.
Upvotes: 0
Reputation: 2932
try this...
Form2 f2 = new Form2();
f2.Show();
f2.SetBounds(this.Location.X, this.Location.Y,this.Width, this.Height);
//this.Hide(); // if you want to hide 1stform after showing 2nd form
Upvotes: 0
Reputation: 942338
Yes, you are doing this the wrong way around. The actual size of the form is only the same as the design size if the machine you run this on has the exact same user preferences, system font size and video DPI setting. If it is off a lot then the DPI setting is different. If it is off a little then the user preferences are different. Like a larger title bar font or bigger buttons. Fix:
Form2 f2 = new Form2();
f2.Show();
f2.Left = this.Left;
f2.Top = this.Top;
f2.Size = this.Size;
If that's too noticeable then you should let the Form2's Load event do this. Pass a reference to your main form or use the Owner property and Show(owner). In other words:
Form2 f2 = new Form2();
f2.Show(this);
in Form2:
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
this.Location = Owner.Location;
this.Size = Owner.Size;
}
Upvotes: 5
Reputation: 4284
What will happen when show your Form2 with the same size and same locations with Form1? Form1 will be invisible right? So, why you dont use form1.Hide();
?
Upvotes: 1
Reputation: 34573
If you don't want the user to interact with Form1 until Form2 is closed, then change your last line to
f2.ShowDialog();
Then it doesn't matter if the user can still see Form1. Windows won't let Form1 get the focus again until Form2 is closed.
Upvotes: 1