如何打开第二种形式? [英] How to open the second form?

查看:144
本文介绍了如何打开第二种形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Form1和Form2在我的项目。窗体2仅仅是一个设置Form1窗体。什么是从Form1中打开窗体2命令,也什么命令来关闭它吗?

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?

推荐答案

您需要处理一个的事件的上提高用户交互的结果Form1上。例如,如果你有一个设置按钮,用户点击以显示设置窗体(Form),你应该处理的点击事件该按钮:

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();
}

在除了 显示方法,你也可以选择使用<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog.aspx"><$c$c>ShowDialog法。所不同的是,后者示出了的形式作为一个的模态对话框,这意味着用户不能与其他形式的相互作用在应用程序中,直到它们关闭模式窗体。这是一个消息框的工作方式相同。该的ShowDialog 方法也会返回一个值,指示如何的形式被关闭了。

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.

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

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.

如果您希望用户要求将其关闭之前,自己关闭它,你可以调用窗体的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx"><$c$c>Close方法:

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

this.Close();

这篇关于如何打开第二种形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆