Showing the second form exactly in place of the first form

From the main form (Form1) I call to show another form (Form2). but I want it to show exactly the same place and size as form1, so that we can no longer see form1 until we close form2 or move it somewhere else. so I wrote the following lines:

        Form2 f2 = new Form2();
        f2.Left = this.Left;
        f2.Top = this.Top;
        f2.Size = this.Size;
        f2.Show();

But he still has problems. form2 we are not completely on form1. any other thing i should add to the code?

+3
source share
5 answers

, . , , , , DPI . , DPI . , . . Fix:

    Form2 f2 = new Form2();
    f2.Show();
    f2.Left = this.Left;
    f2.Top = this.Top;
    f2.Size = this.Size;

, Form2 Load . "" " ()". :

    Form2 f2 = new Form2();
    f2.Show(this);

2:

    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        this.Location = Owner.Location;
        this.Size = Owner.Size;
    }
+2

, Form2 Form1? 1 ? , form1.Hide();?

+1

, Form1 2,

f2.ShowDialog();

, Form1. Windows Form1 , 2 .

0

...

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
0

2 "center on parent"

, 2 1, 1 . , modal (form2.ShowDialog()), 2, .

You can still move form2, as I just mentioned about the form, but this was not indicated as part of this question.

0
source

Source: https://habr.com/ru/post/1783696/


All Articles