2

我需要在 VB.Net vs2008 中打开一个 MDI 子实例...这段代码打开了同一个 MDI 子的多个副本;我得到了这个答案防止c# 的重复 MDI 子窗体,但没有找到 VB.Net vs 2008 的子窗体

Dim myChild As New Form1()
myChild.MdiParent = Me
myChild.Show()
4

3 回答 3

4

这是 Fredrik Mörk 代码的 VB.Net 版本:

For Each f As Form In Application.OpenForms
  If TypeOf f Is Form1 Then
    f.Activate()
    Return
  End If
Next

Dim myChild As New Form1
myChild.MdiParent = Me
myChild.Show()
于 2012-10-11T15:32:40.447 回答
1

尝试这个

private void button1_Click(object sender, EventArgs e)
{
   FormCollection fc = Application.OpenForms;
   bool FormFound = false;
   foreach (Form frm in fc)
   {
      if (frm.Name == "Form2")
      {
          frm.Focus();
          FormFound = true;
      }
   }

   if (FormFound == false)
   {
       Form2 f = new Form2();
       f.Show();
    }
}
于 2013-09-10T12:53:24.703 回答
0

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

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

按如下方式使用它(指示FalsebMultipleInstances防止它们)

Open_MDI(Of Form2)(False)

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

按如下方式使用它(指示falsemultipleInstances防止它们)

OpenMDI<Form2>(false);
于 2018-04-14T00:58:37.043 回答