开发者

Prevent duplicate MDI children forms

开发者 https://www.devze.com 2022-12-08 11:53 出处:网络
Is there a way to prevent the opening of a certain form w开发者_如何学Pythonithin an MDI container if that said form is already opened?You can interate over the OpenForms collection to check if there

Is there a way to prevent the opening of a certain form w开发者_如何学Pythonithin an MDI container if that said form is already opened?


You can interate over the OpenForms collection to check if there is already a form of the given type:

foreach (Form form in Application.OpenForms)
{
    if (form.GetType() == typeof(MyFormType))
    {
        form.Activate();
        return;
    }
}

Form newForm = new MyFormType();
newForm.MdiParent = this;
newForm.Show();


AFAIK there is no standard way. You'll have to implement it yourself. I'd do it this way:

class TheForm: Form
{
    private static TheForm Instance;

    private TheForm() // Constructor is private
    {
    }

    public static Show(Form mdiParent)
    {
        if ( Instance == null )
        {
            // Create new form, assign it to Instance
        }
        else
            Instance.Activate(); // Not sure about this line, find the appropriate equivalent yourself.
    }

    protected override OnFormClose(EventArgs e)
    {
        Instance = null;
        base.OnFormClose(e);
    }
}

If thread safety is of concern, add the appropriate locks.


this code working

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        foreach (Form form in Application.OpenForms)
        {


            if (form.GetType() == typeof(Form2))
            {
                form.Activate();
                return;
            }
        }

        Form2 newForm = new Form2();
        newForm.MdiParent = this;
        newForm.Show();
    }


Though this post is very old, I thought this will add a help.

Need to handle if form is Minimized too. Here is the complete example:

foreach (Form form in this.MdiChildren)
{
    if (form.GetType() == typeof(frmMain))
    {
        if (form.WindowState == FormWindowState.Minimized)
        {
            form.WindowState = FormWindowState.Normal;
        }
        form.Activate();
        return;
    }
}
Form frm = new frmMain();
frm.MdiParent = this;
frm.Show();


This code work for me in vb.net

 

For Each f As Form In Application.OpenForms If TypeOf f Is form_name Then f.Activate() f.WindowState = FormWindowState.Normal f.StartPosition = FormStartPosition.WindowsDefaultLocation f.WindowState = FormWindowState.Maximized

            Return
        End If    
    Next 
    form_name .MdiParent = Me
    form_name .Show()


A method can be implemented using Generics (below C# and VB.net options), which can be useful if different MDI Forms need to be opened.

C#

private void OpenMDI<T>(bool multipleInstances)
    where T : Form, new()
{
    if (multipleInstances == false)
    {
        // Look if the form is open
        foreach (Form f in this.MdiChildren)
        {
            if (f.GetType() == typeof(T))
            {
                // Found an open instance. If minimized, maximize and activate
                if (f.WindowState == FormWindowState.Minimized)
                {
                    f.WindowState = FormWindowState.Maximized;
                }

                f.Activate();
                return;
            }
        }
    }

    T newForm = new T();
    newForm.MdiParent = this;
    newForm.Show();
}

Use it as follows (indicate false in multipleInstances to prevent them)

OpenMDI<Form2>(false);

VB.NET

Public Sub Open_MDI(Of T As {New, Form})(bMultipleInstances As Boolean)
    If bMultipleInstances = False Then
        For Each f As Form In Me.MdiChildren
            If TypeOf f Is T Then
                If (f.WindowState = FormWindowState.Minimized) Then
                    f.WindowState = FormWindowState.Maximized;
                End If

                f.Activate()
                Exit Sub
            End If
        Next
    End If

    Dim myChild As New T()
    myChild.MdiParent = Me
    myChild.Show()
End Sub

Use it as follows (indicate False for bMultipleInstances to prevent them)

Open_MDI(Of Form2)(False)


This code work for me in C#

      private void btn1_Click(object sender, EventArgs e)
            {
               Form2 new_form = new Form2();
                if (new_form.visible)
                    new_form.Show();
                else
                    new_form.ShowDialog();
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消