I want to position two forms right next to each other in C#
I have one form which opens another form.
What I want to do is position the newly opened form right next (on the right side) to the already visible form.
So I need to position the new form to where the current form ends (correct me if wrong).
So I will need to do something like:
newform.Left = this.endposition.right;
The endposition property is something I just made up (pseudo code).
How to get the end position on the right side of the current form?
EDIT
I've tried several solutions but until now none of them worked.
I always get the same result:
开发者_JAVA百科I've tried the following codes:
newform.Left = this.Right + SystemInformation.BorderSize.Width;
newform.Left = this.Right + (SystemInformation.BorderSize.Width * 2);
newform.SetDesktopLocation(this.Location.X + this.Size.Width, this.Location.Y);
Eclyps19 suggested to just manually add some pixels to position the new form, although I'm not sure whether the border will be the same on every system.
This works for me:
Form1 nForm = new Form1();
nForm.Show();
nForm.SetDesktopLocation(this.Location.X + this.Size.Width, this.Location.Y);
Try
newform.Left = oldform.Right + SystemInformation.BorderSize.Width;
This should add the width (in pixels) of the border to the oldform.Right value. You can replace (or add to) SystemInformation.BorderSize.Width with any integer you'd like to fit your liking.
I Know this question is old, but for anyone coming here looking for an answer, you need to take into account the size of both form's borders. The border size will be different depending on the FormBorderStyle
.
var newForm = new Form2();
newForm.Show();
var form1Border = (this.Width - this.ClientSize.Width) / 2;
var newFormBorder = (newForm.Width - newForm.ClientSize.Width) / 2;
newForm.Left = this.Left + this.Width - form1Border - newFormBorder;
Your code works fine, but you just have to put it AFTER your newForm.Show() method.
精彩评论