在c#窗口中,当我通过enother形式调用一个 [英] in c# windows when i call one for through enother form

查看:95
本文介绍了在c#窗口中,当我通过enother形式调用一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式.它们是Form1和Form2.我的要求是,当我在Form1的按钮单击事件中调用Form2时,Form1将被关闭,而Form2将仅显示.

我在按钮Click事件下编写了这样的代码:

i have two forms. Those are Form1 and Form2. My requirement is when i call Form2 in button click event of Form1, then Form1 will get Closed and Form2 will only be displayed.

I wrote code like this, under button Click event:

Form2 F2 = new Form2();
F2.ShowDialog();
this.Close();


这段代码有效,但是当Form2也关闭时,表单也将关闭.


this code is working but Forms will Close When Form2 is also Closed.

推荐答案

我猜这"是您的主要表单,由"main"启动.这样,当您关闭它时,您将关闭程序.它为您提供了两种选择,可以从单独的表单(可能是隐藏的)运行应用程序,或者直接隐藏主表单.

I am guessing "this" is your main form, the one launched by "main". As such, when you close it, you close your program. It leaves you with 2 choices, run your application from a separate form (that is probably hidden), or simply HIDE the main form thusly.

this.Hide()
Form2 F2 = new Form2();
F2.ShowDialog();


您可以尝试:
You may try:
static void Main()
{
    //Application.Run(new Form1());
    new Form1.ShowDialog();
}


就像Alaric Dailey所说的一样,但您需要了解一些内容
当您退出应用程序时

是行不通的 this.close(this mean form2)
因为form1仍在运行但处于隐藏模式
所以您需要这样做
在form1(主要形式)中写:
like what Alaric Dailey said but there is something you need to know
when you wnat to exit from the application
it is not work to write
this.close(this mean form2)
because form1 is still running but in hide mode
so you need to do this
in form1(main form) write :
public void exitfuntion()
{
this.application.exist();
}


并以form2
form1 mainform=new form1()

并在需要退出应用程序时执行此操作

mainform.exitfunction();

您可以从任务管理器"中观看所有这一切

祝你好运


and in form2
form1 mainform=new form1()

and when you need to exit application do this

mainform.exitfunction();

you can watch all this from "taskmanager"

good luck


这篇关于在c#窗口中,当我通过enother形式调用一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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