简体   繁体   English

更改活动表单以显示另一种表单

[英]change active form to show another form

I have a Form1 and another one that I added. 我有一个Form1和另一个我添加的。 Form1 is being run by program.cs at the start. Form1在开始时由program.cs运行。 I need to hide Form1 and show options form by the press of a button. 我需要通过按下按钮隐藏Form1并显示options表单。

    private void submitPassword_Click(object sender, EventArgs e)
    {
        options optionForm = new options();
        optionForm.Show();
    }

the above code opens the options form on top, but I need it to replace the current form. 上面的代码在顶部打开选项表单,但我需要它来替换当前表单。 how can I achieve this? 我该怎么做到这一点?

在显示新窗体之前使用this.Close()隐藏当前窗体并确保使用无参数Application.Run因此当您关闭它的主窗体时,程序不会关闭。

You can use "Hide" and "Show" method : 您可以使用“隐藏”和“显示”方法:

private void submitPassword_Click(object sender, EventArgs e)

{

    options optionForm = new options();

    this.Hide();

    optionForm.Show();
}
private void submitPassword_Click(object sender, EventArgs e)
{
    options optionForm = new options();
    optionForm.Show();
    this.Hide();
}

Similar solutions where one form calls and acts on another... Such as this one I answered for another. 类似的解决方案,其中一个表单调用并作用于另一个...就像这一个我回答另一个。 You could do a similar process... pass in your first form to the second... Then show the second... Then, you could HIDE your first form (via this.Hide() ). 你可以做一个类似的过程...将你的第一个表格传递给第二个...然后显示第二个...然后,你可以隐藏你的第一个表格(通过this.Hide())。 Then, in your second form, when you click whatever button to select your choice, and need to return back to the first form, you could then use the original form's reference passed INTO the second form to re-Show it, such as in the click on the second form... 然后,在您的第二个表单中,当您单击任何按钮以选择您的选择,并且需要返回到第一个表单时,您可以使用原始表单的引用传入INTO第二个表单来重新显示它,例如在点击第二张表格......

this.PreservedForm.Show();  // re-show original form
this.Close();   // and CLOSE this second form...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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