简体   繁体   中英

Visual Studio C# Opening forms in the same window

I'm sorry for the probably misleading title, but I couldn't think of anything better. So I want to create an application in which when a button is pressed, it opens another form, but it will replace the original form. I need something that many other programs have. It just replaces the data in form with the 2nd forms data. The second form will be the same as the previous just with other content - the same position, same size.. everything should be same. Sorry for my bad english and thank you for reading.

I don't know why you make this program, I want to help below code to you.

private void button1_Click(object sender, EventArgs e)
{
    // #1. Make second form
    // If you want to make equivalent one, then change Form2 -> Form1
    Form2 form2 = new Form2();

    // #2. Set second form's size
    form2.Width = this.Width;
    form2.Height = this.Height;

    // #3. Set second form's start position as same as parent form
    form2.StartPosition = FormStartPosition.Manual;
    form2.Location = new Point(this.Location.X, this.Location.Y);

    // #4. Set parent form's visible to false
    this.Visible = false;

    // #5. Open second dialog
    form2.ShowDialog();

    // #6. Set parent form's visible to true
    this.Visible = true;
}

Ok so I finally got it working with panels. My explanation skills are very poor. Anyways, thanks for you time!

on your project click add then user control then add new form

on your code when the botton is pressed add

Form2 form = new Form2();
                form.ShowDialog();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM