防止重复的 MDI 子表单

发布于 2024-08-08 07:39:35 字数 40 浏览 4 评论 0原文

如果 MDI 容器中的某个表单已经打开,是否有办法阻止打开该表单?

Is there a way to prevent the opening of a certain form within an MDI container if that said form is already opened?

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

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

发布评论

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

评论(8

染年凉城似染瑾 2024-08-15 07:39:35

您可以对 OpenForms 集合进行交互以检查是否已经存在给定类型的表单:

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

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

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();
書生途 2024-08-15 07:39:35

AFAIK 没有标准方法。您必须自己实施它。我会这样做:

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);
    }
}

如果关心线程安全,请添加适当的锁。

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.

分分钟 2024-08-15 07:39:35

这段代码工作

    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();
    }

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();
    }
坠似风落 2024-08-15 07:39:35

虽然这篇文章很旧,但我认为这会有所帮助。

如果表单也最小化,则需要处理。这是完整的示例:

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();

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();
醉态萌生 2024-08-15 07:39:35

这段代码在 vb.net 中对我有用

 

对于 Application.OpenForms 中的每个 f 作为表单 如果 TypeOf f 是 form_name 那么 f.激活() f.WindowState = FormWindowState.Normal f.StartPosition = FormStartPosition.WindowsDefaultLocation f.WindowState = FormWindowState.Maximized

            Return
        End If    
    Next 
    form_name .MdiParent = Me
    form_name .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()

面犯桃花 2024-08-15 07:39:35

可以使用泛型(在 C# 和 VB.net 选项下方)实现方法,如果需要打开不同的 MDI 表单,这会很有用。

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();
}

按如下方式使用(在 multipleInstances 中指示 false 以防止出现)

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

按如下方式使用(为 bMultipleInstances 指示 False 以防止它们)

Open_MDI(Of Form2)(False)

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)
红焚 2024-08-15 07:39:35

这段代码在 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();
            }

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();
            }
所有深爱都是秘密 2024-08-15 07:39:35
    [STAThread]
    static void Main()
    {
        bool createdNew;
        using (Mutex mutex = new Mutex(true, "YourUniqueMutexName", out createdNew))
        {
            if (createdNew)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
            else
            {
                Process currentProcess = Process.GetCurrentProcess();
                foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
                {
                    if (process.Id != currentProcess.Id)
                    {
                        SetForegroundWindow(process.MainWindowHandle);
                        break;
                    }
                }
            }
        }
    }
    [STAThread]
    static void Main()
    {
        bool createdNew;
        using (Mutex mutex = new Mutex(true, "YourUniqueMutexName", out createdNew))
        {
            if (createdNew)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
            else
            {
                Process currentProcess = Process.GetCurrentProcess();
                foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
                {
                    if (process.Id != currentProcess.Id)
                    {
                        SetForegroundWindow(process.MainWindowHandle);
                        break;
                    }
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文