I want to position two forms right next to each other in C#
Asked Answered
A

5

7

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:

Why does this happen

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.

Alialia answered 26/8, 2011 at 17:43 Comment(0)
A
10

This works for me:

        Form1 nForm = new Form1();
        nForm.Show();
        nForm.SetDesktopLocation(this.Location.X + this.Size.Width, this.Location.Y);
Appeasement answered 26/8, 2011 at 18:20 Comment(2)
did not work, Isnt there any other settings that I have to toggle?Idealist
I fixed my issue by calling " int _left = this.Parent.Location.X + this.Parent.Size.Width; int _top = this.Parent.Location.Y;"Idealist
L
3

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.

Lambrecht answered 26/8, 2011 at 17:44 Comment(9)
@Eclypse19: Ok getting close: however it doesn't take into account the border of the form.Alialia
You can always add an additional value to it - try newform.left = oldform.right + oldform.BorderSizeLambrecht
@Eclypse19: .Right = same problem. And there is no .BorderSize property.Alialia
Woops - disregard my last comment. Try adding SystemInformation.Bordersize to the oldform.Right. This will pull the system's border size value in pixels and add it to the .Right value.Lambrecht
@Eclypse19: It still overlapses a tiny bit when I use: newform.Left = this.Right + SystemInformation.BorderSize.Width; I also tried: preview.Left = this.Right + (SystemInformation.BorderSize.Width * 2); which also doesn't work :(Alialia
If you need to make slight adjustments, just increase the extra pixels being added to it with a +1 or +2 or +3 etc.Lambrecht
@Eclypse19: I thought about it. However I am worried that the difference would be different from system to system. If you are sure that will not be the case I will go down that route.Alialia
Try SizingBorderWidth instead of BorderSize.Appeasement
The system-to-system changes will be carried over with the SystemInformation.BorderSize.Width. It sounds like the additional adjustment is strictly your own preference, and SHOULD match from system to system. Are you sure the slight overlap is not due to one being selected (and thus possibly having a deeper border)?Lambrecht
U
1

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;
Ursine answered 18/9, 2020 at 7:35 Comment(0)
U
0

Your code works fine, but you just have to put it AFTER your newForm.Show() method.

Upshaw answered 23/2, 2017 at 14:0 Comment(0)
C
0

@Greg-Andora

Thanks for Your answer it was very useful ,But there is small Note about the Form it self

1-The Developer must Change property of Form (start Position: Manual )

Or it will Start at it's Standard Position

2-assume to have one Form calling Two Forms so we make an Object of every Form then call every one inside one Control(Button as Example)

Important Note:- there is something to Deal with that the new position will be about 15 inches (between every last form and The New Form ) so we have to less them from the new Form

so The Code for calling Forms is

Form2 form2 = new Form2();
Form3 form3 = new Form3();

private void Btn_CallForm2_Click(object sender, EventArgs e)
{
    form2.StartPosition = FormStartPosition.Manual;
    form2.Location = new Point(this.Location.X+form2.Width-15, this.Location.Y);
    form2.Show();
}

private void Btn_CallForm3_Click(object sender, EventArgs e)
{
    form3.StartPosition = FormStartPosition.Manual;
    form3.Location = new Point((this.Location.X + form2.Width +form3.Width)- 30, this.Location.Y);
    form3.Show();
}
enter code here

============= Note that for calling Form2 we make it less with 15 and For calling Form3 we make it less with 30

======

Cordiality answered 19/7, 2024 at 11:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.