如何打开第二个表格?

发布于 2024-11-02 20:49:02 字数 88 浏览 6 评论 0原文

我的项目中有 Form1 和 Form2。 Form2 只是一个具有 Form1 设置的表单。从 Form1 打开 Form2 的命令是什么,关闭它的命令是什么?

I have Form1 and Form2 in my project. Form2 is just a form with settings for Form1. What is the command to open the Form2 from the Form1 and also what's the command to close it please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

冰魂雪魄 2024-11-09 20:49:02

您需要处理 Form1 上由于用户交互而引发的事件。例如,如果您有一个“设置”按钮,用户单击该按钮以显示设置表单 (Form2),则您应该处理该按钮的 Click 事件:

private void settingsButton_Click(Object sender, EventArgs e)
{
    // Create a new instance of the Form2 class
    Form2 settingsForm = new Form2();

    // Show the settings form
    settingsForm.Show();
}

除了 显示方法,您还可以选择使用 ShowDialog 方法。不同之处在于,后者将表单显示为模态对话框,这意味着用户在关闭模态表单之前无法与应用程序中的其他表单进行交互。这与消息框的工作方式相同。 ShowDialog 方法还返回一个值,指示表单如何关闭。


当用户关闭设置表单(例如,通过单击标题栏中的“X”)时,Windows 将自动关闭它。

如果您想在用户要求关闭之前自行关闭它,可以调用表单的 关闭方法

this.Close();

You need to handle an event on Form1 that is raised as a result of user interaction. For example, if you have a "Settings" button that the user clicks in order to show the settings form (Form2), you should handle the Click event for that button:

private void settingsButton_Click(Object sender, EventArgs e)
{
    // Create a new instance of the Form2 class
    Form2 settingsForm = new Form2();

    // Show the settings form
    settingsForm.Show();
}

In addition to the Show method, you could also choose to use the ShowDialog method. The difference is that the latter shows the form as a modal dialog, meaning that the user cannot interact with the other forms in your application until they close the modal form. This is the same way that a message box works. The ShowDialog method also returns a value indicating how the form was closed.


When the user closes the settings form (by clicking the "X" in the title bar, for example), Windows will automatically take care of closing it.

If you want to close it yourself before the user asks to close it, you can call the form's Close method:

this.Close();
眸中客 2024-11-09 20:49:02

如果您想以模态方式打开Form2(意味着在Form2打开时您无法单击Form1),您可以这样做:

using (Form2 f2 = new Form2()) 
{
    f2.ShowDialog(this);
}

如果您想以非模态方式打开Form2(意味着您仍然可以单击Form1(当 Form2 打开时),您可以创建对 Form2 的表单级引用,如下所示:

private Form2 _f2;

public void openForm2()
{
    _f2 = new Form2();
    _f2.Show(this); // the "this" is important, as this will keep Form2 open above 
                    // Form1.
}

public void closeForm2()
{
    _f2.Close();
    _f2.Dispose();
}

If you want to open Form2 modally (meaning you can't click on Form1 while Form2 is open), you can do this:

using (Form2 f2 = new Form2()) 
{
    f2.ShowDialog(this);
}

If you want to open Form2 non-modally (meaning you can still click on Form1 while Form2 is open), you can create a form-level reference to Form2 like this:

private Form2 _f2;

public void openForm2()
{
    _f2 = new Form2();
    _f2.Show(this); // the "this" is important, as this will keep Form2 open above 
                    // Form1.
}

public void closeForm2()
{
    _f2.Close();
    _f2.Dispose();
}
从﹋此江山别 2024-11-09 20:49:02
//To open the form

Form2 form2 = new Form2();

form2.Show();
// And to close
form2.Close();

希望这有帮助

//To open the form

Form2 form2 = new Form2();

form2.Show();
// And to close
form2.Close();

Hope this helps

走过海棠暮 2024-11-09 20:49:02

我假设您谈论的是 Windows 窗体:

要显示窗体,请使用 Show() 方法:

Form form2 = new Form();
form2.Show();

要关闭窗体,请使用 Close():

form2.Close();

I assume your talking about windows forms:

To display your form use the Show() method:

Form form2 = new Form();
form2.Show();

to close the form use Close():

form2.Close();
树深时见影 2024-11-09 20:49:02

一句话就是:

(new Form2()).Show();

希望有帮助。

In a single line it would be:

(new Form2()).Show();

Hope it helps.

说谎友 2024-11-09 20:49:02

在任何点击事件(或其他事件)上:

Form2 frm2 = new Form2();
frm2.Show();

On any click event (or other one):

Form2 frm2 = new Form2();
frm2.Show();
鹤舞 2024-11-09 20:49:02

分别 Form.Show() (或 Form.ShowDialog()(如果您希望第二个表单为模式),以及 Form.Hide() (或 Form.Close(),具体取决于什么你的意思是关闭它)。

Respectively Form.Show() (or Form.ShowDialog() if you want the second form to be modal), and Form.Hide() (or Form.Close(), depending on what you mean by close it).

梦回旧景 2024-11-09 20:49:02

如果您需要将 Form2 显示为 模式对话框,请从 < code>Form1 do:

var form2 = new Form2();
if (form2.ShowDialog() == DialogResult.OK) 
{
    // process results here
}

模式对话框在打开时将保留焦点;它会“在后台”设置父窗口 (Form1),直到其关闭,这是设置窗口的常见做法。

If you need to show Form2 as a modal dialog, from within Form1 do:

var form2 = new Form2();
if (form2.ShowDialog() == DialogResult.OK) 
{
    // process results here
}

A modal dialog will retain focus while it is open; it will set the parent windows (Form1) "in the background" until it is closed, which is quite a common practice for settings windows.

剧终人散尽 2024-11-09 20:49:02

从这个开始:

var dlg = new Form2();
dlg.ShowDialog();

Start with this:

var dlg = new Form2();
dlg.ShowDialog();
梦魇绽荼蘼 2024-11-09 20:49:02
Form1 OpenNewForm = new Form1();
OpenNewForm.Show();

“OpenNewForm”是表单的名称。
第二步,表格打开。

如果你想关闭之前的表单:

this.Close();
Form1 OpenNewForm = new Form1();
OpenNewForm.Show();

"OpenNewForm" is the name of the Form.
In the second the form opens.

If you want to close the previous form:

this.Close();
昇り龍 2024-11-09 20:49:02

在 C#.Net 中,如果您位于解决方案内部和项目内部,并且 Form2(带有 Form2.cs)位于 内部,

 private void button1_Click(object sender, EventArgs e)
    {
       ProjectName.Form2 secondaryForm= new ProjectName.Form2();

       secondaryForm.Show();
    }

In C#.Net if you're inside of a Solution, and inside of a Project, and Form2 (with Form2.cs) is inside of ,

 private void button1_Click(object sender, EventArgs e)
    {
       ProjectName.Form2 secondaryForm= new ProjectName.Form2();

       secondaryForm.Show();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文