C# 从主窗体打开现有窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10448951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:56:15  来源:igfitidea点击:

Open an existing form from the main form

c#winforms

提问by

I designed two forms: Form1and Form2. Form1is the main form. There is a button in Form1, if I click the button, then Form2will pop out. I want to do something on Form2.

我设计了两种形式:Form1Form2Form1是主要形式。中有一个按钮Form1,如果我单击该按钮,Form2则会弹出。我想做点什么Form2

// click button in Form1.
private void button1_Click(object sender, EventArgs e)
    {
        Form form2= new Form();
        form2.ShowDialog();
    }

But Form2is a new form rather than an existing form.

但是Form2是一种新形式而不是现有形式。

It is wrong.

这是错误的。

How? Thanks.

如何?谢谢。

采纳答案by Adil

You are creating instance of Form class not the Form2 which you have in your project. Create instance of Form2 which you created earlier and then call ShowDialog in it.

您正在创建 Form 类的实例,而不是您项目中的 Form2。创建您之前创建的 Form2 实例,然后在其中调用 ShowDialog。

You might have notice the in the program.cs something like Application.Run(new Form1()); Here we create the instance of Form1 and pass to Run method.

您可能已经注意到 program.cs 中的类似 Application.Run(new Form1()); 这里我们创建 Form1 的实例并传递给 Run 方法。

Do it this way by creating instance of Form2 and calling ShowDialog() method to show it

通过创建 Form2 的实例并调用 ShowDialog() 方法来显示它来做到这一点

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

回答by Tigran

Declare

宣布

Form2 form2= new Form2();

like your class member and use it like this:

像你的班级成员一样使用它:

private void button1_Click(object sender, EventArgs e)
{

        form2.ShowDialog(); //blocking call 

       //or form2.Show() //non blocking call
}

EDIT

编辑

Based on correct comments, to make this work instead of executing the Close()on the function which will lead to Dispose()you need to use form2.Hide()to make is simply invisible

基于正确的注释,要使这项工作而不是执行Close()on 将导致Dispose()您需要使用form2.Hide()make 的函数根本不可见

回答by igofed

You create blank form with

您创建空白表单

Form Form2= new Form();

You should use

你应该使用

Form2 form2= new Form2();

Complete code:

完整代码:

private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2= new Form2();
        form2.ShowDialog();
    }

回答by Shefat

 private void button1_Click(object sender, EventArgs e)
        {
            InputForm form1 = new InputForm();
            form1.Show();
        }

Here InputForm means which form you want to open.

此处 InputForm 表示您要打开的表单。

回答by KaizenLouie

The question is "Open an existing form from the main form"

问题是“从主表单打开现有表单”

Okay lets change it a little, Open an existing instance of form from the main form.

好的,让我们稍微改变一下,从主窗体打开一个现有的窗体实例。

when you show a form

当你展示一个表格时

new Form2().Show();

lets say you hidit using

让我们说你隐藏它使用

Form2.Hide();

you guys canuse this

你们可以用这个

var Form2_instance = Application.OpenForms.OfType<Form2>().Single();
Form2_instance.Show();