表格配置在csharp中 [英] form dispose in csharp

查看:67
本文介绍了表格配置在csharp中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai
我的应用程序中有两种形式.
使用计时器在第一种形式出现后几秒钟显示第二种形式.
现在,我需要第一种形式在第二种形式出现时自动关闭.

hai
I have two forms in my app.
The second form is shown a few second after the first form appear using timer.
Now i need the first form to close automatically when the second form appear.
How this can be done?

推荐答案

您不能在System.Windows.Forms.Application实例的单个运行时执行此操作. 当您调用Application.Run时,其参数定义了System.Windows.Forms.Form的实例,该实例承担了主窗体的作用.所有其他形式均为非主要形式.每次关闭主窗体时,应用程序都会关闭.这意味着Application.Run退出.除非您显式调用Application.Exit,否则任何其他形式都不会出现Is.

因此,原则上,您可以在应用程序的入口点内使用棘手的技术(通常是Main方法):

You cannot do it during the single run-time of the instance of System.Windows.Forms.Application.
When you call Application.Run, its parameter defined the instance of the System.Windows.Forms.Form which assumes the role of Main Form. All other forms will be non-main. Every time you close the main form, Application is closed. It means, Application.Run exits. Is does not happens with any other form unless you explicitly call Application.Exit.

So, in principle you can use tricky technique inside your application''s entry point (normally, this is the Main method):

static void Main() {
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   Form firstMain = new FirstMain();
   Form secondMain = new SecondMain();

   // inside this call firstMain may or may not show secondMain,
   // close itself and so exit Run method:
   Application.Run(firstMain);

   if (CertainConditionYouMightDesign(firstMain)
       Applicatin.Run(secondMain);
} //Main



当然可以,但是... 不要这样做
这将严重滥用Forms应用程序的预期用途.

您可以模仿所需的效果隐藏表单而不是关闭表单.自然,您需要保留一种明确退出应用程序的方法.每次关闭表单时,都需要确保显示另一个表单.这种技术是最常用和最成功的.

这是在尝试关闭表单时隐藏表单的方法:



This certainly will work but… don''t do it!
This will be a pretty big abuse of the intended use of the Forms application.

You can imitate desired effect hiding forms instead of closing. Naturally, you need to preserve a way to exit application explicitly. Every time the form is closed, you need to make sure another form is shown. This technique is the most usual and successful.

This is how to hide a form on attempt to close it:

class SecondMain : Form {

//...

    protected override void OnFormClosing(FormClosingEventArgs e) {
        if (e.CloseReason == CloseReason.UserClosing) {
            e.Cancel = true;
            Hide();
        } //if
    } //OnFormClosing

} //class SecondMain



我没有展示自动关闭一种形式并展示另一种形式的逻辑.基本上,您应该创建一些接口并在每种表单上实现它.该界面应具有一种或两种在表单之间进行通信的方法,因此表单会相互告知:我正在关闭,请展示自己并转到顶部".在表单生命周期的开始,您需要相互传递另一个表单的接口引用. (您可以直接将表单的实例彼此传递,并具有一些用于此类交互的公共方法,但这违反了隔离原则;委托是最安全的技术;您不应传递真正需要的更多访问权限.)

由于使用了partial类声明,因此该技术特别方便.您可以为同一类创建更多单独的文件,并添加一个专门用于此效果的局部声明.有关表单协作的更多信息,请在这里



I''m not showing the logic of automatic closing one form and showing another one. Basically, you should create some interface and implement it on each form. This interface should have one or two methods for communications between forms, so the form would tell to each other: "I''m closing, you show yourself and go to top". In the beginning of the forms life cycle, you need to pass the interface reference for the other form to each other. (You could directly pass instances of the forms to each other and have some public methods for such interaction, but this is a violation of isolation principles; delegates is the most safe technique; you should not pass more access then it''s really needed.)

This technique is especially convenient thanks to using of partial class declaration. You can create more separate files for the same class and add one more partial declaration devoted to this effect. For more information on form collaboration, see my past answer here How to copy all the items between listboxes in two forms[^]. Consider other solutions and see the discussion.

Now, final advice: do not do it all! Change the general design. Make one and only one main form, fill it with some TabControl and make some tabs. What were you separate windows make a separate tab pages. Simple and civilized. More than one form is not really nice solution.

—SA


显示第二个表单,然后隐藏第一个表单.

Show the second form and then hide the first form.

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

this.hide()


这篇关于表格配置在csharp中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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