在C#Windows应用程序中找到打开的表单 [英] Find the open forms in c# windows application

查看:62
本文介绍了在C#Windows应用程序中找到打开的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此功能关闭现有表单并打开新表单.

I am using this function to close existing form and open a new form.

如果没有拼写形式,则会引发错误.

If there is no exixting form, it throws error.

错误:

目标:System.Object MarshaledInvoke(System.Windows.Forms.Control,System.Delegate,System.Object [],布尔值)

Target : System.Object MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)

消息:在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke.

Message : Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

Stack:位于System.Windows.Forms.Control.MarshaledInvoke(控制调用方,委托方法,Object [] args,布尔同步)

Stack : at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)

因此,在关闭表单之前,需要检查是否有任何打开的表单,以避免错误.怎么样?

SO need to check for any form open before closing the form to avoid the error. How?

    static public void NewMainForm(Form main, bool ClosePreviousMain)
    {
            if (main != null)
            {
                Global.ActiveForm = main.Text;
                if (ClosePreviousMain & MyContext.curMain != null)
                {
                    MyContext.curMain.FormClosed -= new FormClosedEventHandler(main_FormClosed);
                    //Need to check for any form active and then close the form.
                    MyContext.curMain.Invoke(new Action(MyContext.curMain.Dispose));
                }
                MyContext.curMain = main;
                MyContext.curMain.FormClosed += new FormClosedEventHandler(main_FormClosed);
                MyContext.curMain.ShowDialog();
            }
    }

推荐答案

您可以使用 查看全文

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