封闭式 [英] close form

查看:87
本文介绍了封闭式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何关闭Windows应用程序窗体并打开另一个Windows应用程序窗体并重复它?

例如当我写

How do I close a windows app form and open another windows app form and repeat it?

When I write for example

form1 frm= new form1();
frm.close();
form2 frm2=new form2();
frm2.showdialog();




form1尚未关闭,但form2已打开.




form1 isn''t close but form2 opened.

推荐答案

因为您声明了form1的新实例并立即将其关闭:
Because you declare a new instance of form1 and immediately close it:
form1 frm= new form1();
frm.close();

什么都没发生-它没有关闭,因为您从未打开过它.
如果您在关闭当前实例时正在运行在form1中(这似乎是您试图执行的操作),则您的应用程序将关闭.

尝试以下方法:

Nothing happens - it doesn''t close because you never opened it.
If you are running in form1 when you close the current instance (which is what you appear to be trying to do) then your app will shut down.

Try this instead:

this.Hide();
Form2 frm2 = new Form2():
Form2.ShowDialog();
this.Show();

它将使您看起来好像您的表单消失了,而新表单又出现了.
您实际上并不需要隐藏或显示"的this部分-我只是将它们放入其中以向您展示它们的作用.

It will make it look as if your form disappears and the new form appears.
You don''t actually need the this part of Hide or Show - I just put them in to show you what they were acting on.


大多数Windows窗体应用程序都包含一个类Program ,通常在Program.cs中定义,类似于

Most windows forms applications includes a class Program, often defined in Program.cs similar to

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }



Main是应用程序的入口点,Application.Run(new MainForm());创建并显示MainForm的实例.

我希望你可以从这里拿走:)

更新:
Application.Run还负责执行Windows消息循环...

问候
Espen Harlinn



Main is the entry point of you application, and Application.Run(new MainForm()); creates and displays an instance of MainForm.

I expect you can take it from here :)

Update:
Application.Run is also responsible for executing the Windows message loop ...

Regards
Espen Harlinn


您是否在谈论MDI应用程序?如果是,则必须设置form1的IsMdiContainer属性以使其成为多个子窗体的容器

Are you talking about MDI application?If so,You have to set the IsMdiContainer property of form1 to make it as the container for multiple child forms

this.IsMDIContainer = true;


//To Create MDI Child Forms,say your form2

Form frmchild=new Form();
frmchild.MDIParent=this;
frmchild.Show();


检查一下:
使用C#(演练)创建MDI应用程序 [


Check this :
Creating MDI application using C# (Walkthrough)[^]


这篇关于封闭式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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