限制 MDI 应用程序中窗口实例的数量

发布于 2024-09-30 15:18:03 字数 95 浏览 6 评论 0原文

我想限制用户在 MDI 应用程序中创建表单的多个实例。

如果打开该窗体的一个实例,它必须获得焦点。如果它不是新实例,则必须创建它。

我该怎么做?

I want to restrict the user to create multiple instances of a form in an MDI application.

If one instance of that form is opened it must get focus. If it is not a new instance it must be created.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀梦 2024-10-07 15:18:03

你可以这样做。

创建一个静态方法:

public static Form IsFormAlreadyOpen(Type FormType)
{
    foreach (Form OpenForm in System.Windows.Forms.Application.OpenForms)
    {
        if (OpenForm.GetType() == FormType)
            return OpenForm;
    }

    return null;
}

然后当您创建子表单时。

frmMyChildForm frmChild1;

 if ((frmChild1 = (frmMyChildForm)IsFormAlreadyOpen(typeof(frmMyChildForm))) == null)
    { //Form isn't open so create one
        frmChild1= new frmMyChildForm ();

    }
   else
    { // Form is already open so bring it to the front
       frmChild1.BringToFront();

     }

You can do it like this.

Create a static Method:

public static Form IsFormAlreadyOpen(Type FormType)
{
    foreach (Form OpenForm in System.Windows.Forms.Application.OpenForms)
    {
        if (OpenForm.GetType() == FormType)
            return OpenForm;
    }

    return null;
}

And then when you create your child form.

frmMyChildForm frmChild1;

 if ((frmChild1 = (frmMyChildForm)IsFormAlreadyOpen(typeof(frmMyChildForm))) == null)
    { //Form isn't open so create one
        frmChild1= new frmMyChildForm ();

    }
   else
    { // Form is already open so bring it to the front
       frmChild1.BringToFront();

     }
神也荒唐 2024-10-07 15:18:03

您可以使用单例模式方法,并让表单具有一个实例成员变量来跟踪它是否已初始化。

http://en.wikipedia.org/wiki/Singleton_pattern

You could use a singleton-pattern-approach, and let the form have an Instance-member-variable that keeps track of whether it's been initialized or not.

http://en.wikipedia.org/wiki/Singleton_pattern

匿名的好友 2024-10-07 15:18:03

也许这样的东西可以帮助你,

Form frmToCreate;
String strClassName=typeof(FormToCreate).Name
frmToCreate = GetForm(strClass);
if(frmToCreate == null)
{
    //create the form here
}
frmToCreate.MdiParent = this; //supposing you are inside of the mainwindow (MDI window)
frmToCreate.Visible = true;
//other code goes here

GetForm 就会是这样的。

public Form GetForm(String type)
{
    int i;
    Form[] children = this.MdiChildren; //or mdiwindow.MdiChildren

    for (i = 0; i < children.Length; i++)
    {
        if (children[i].GetType().Name == type)
        {
            return children[i];
        }
    }
    return null;
}

如果只是使用 MdiChildren 属性,

Maybe something like this could help you

Form frmToCreate;
String strClassName=typeof(FormToCreate).Name
frmToCreate = GetForm(strClass);
if(frmToCreate == null)
{
    //create the form here
}
frmToCreate.MdiParent = this; //supposing you are inside of the mainwindow (MDI window)
frmToCreate.Visible = true;
//other code goes here

where GetForm would be something like this

public Form GetForm(String type)
{
    int i;
    Form[] children = this.MdiChildren; //or mdiwindow.MdiChildren

    for (i = 0; i < children.Length; i++)
    {
        if (children[i].GetType().Name == type)
        {
            return children[i];
        }
    }
    return null;
}

If just a matter of playing with MdiChildren property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文