如何打开第二个表格?
我的项目中有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您需要处理 Form1 上由于用户交互而引发的事件。例如,如果您有一个“设置”按钮,用户单击该按钮以显示设置表单 (Form2),则您应该处理该按钮的
Click
事件:除了
显示
方法,您还可以选择使用ShowDialog
方法。不同之处在于,后者将表单显示为模态对话框,这意味着用户在关闭模态表单之前无法与应用程序中的其他表单进行交互。这与消息框的工作方式相同。ShowDialog
方法还返回一个值,指示表单如何关闭。当用户关闭设置表单(例如,通过单击标题栏中的“X”)时,Windows 将自动关闭它。
如果您想在用户要求关闭之前自行关闭它,可以调用表单的
关闭
方法: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:In addition to the
Show
method, you could also choose to use theShowDialog
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. TheShowDialog
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:如果您想以模态方式打开
Form2
(意味着在Form2打开时您无法单击Form1),您可以这样做:如果您想以非模态方式打开Form2(意味着您仍然可以单击Form1(当 Form2 打开时),您可以创建对 Form2 的表单级引用,如下所示:
If you want to open
Form2
modally (meaning you can't click on Form1 while Form2 is open), you can do 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:
希望这有帮助
Hope this helps
我假设您谈论的是 Windows 窗体:
要显示窗体,请使用 Show() 方法:
要关闭窗体,请使用 Close():
I assume your talking about windows forms:
To display your form use the Show() method:
to close the form use Close():
一句话就是:
希望有帮助。
In a single line it would be:
Hope it helps.
在任何点击事件(或其他事件)上:
On any click event (or other one):
分别 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
).如果您需要将
Form2
显示为 模式对话框,请从 < code>Form1 do:模式对话框在打开时将保留焦点;它会“在后台”设置父窗口 (
Form1
),直到其关闭,这是设置窗口的常见做法。If you need to show
Form2
as a modal dialog, from withinForm1
do: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.从这个开始:
Start with this:
“OpenNewForm”是表单的名称。
第二步,表格打开。
如果你想关闭之前的表单:
"OpenNewForm" is the name of the Form.
In the second the form opens.
If you want to close the previous form:
在 C#.Net 中,如果您位于解决方案内部和项目内部,并且 Form2(带有 Form2.cs)位于 内部,
In C#.Net if you're inside of a Solution, and inside of a Project, and Form2 (with Form2.cs) is inside of ,