我需要在 VB.Net vs2008 中打开一个 MDI 子实例...这段代码打开了同一个 MDI 子的多个副本;我得到了这个答案防止c# 的重复 MDI 子窗体,但没有找到 VB.Net vs 2008 的子窗体
Dim myChild As New Form1()
myChild.MdiParent = Me
myChild.Show()
这是 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()
尝试这个
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();
}
}
可以使用泛型(在 C# 和 VB.net 选项下方)实现方法,如果需要打开不同的 MDI 表单,这可能很有用。
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
按如下方式使用它(指示False
以bMultipleInstances
防止它们)
Open_MDI(Of Form2)(False)
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();
}
按如下方式使用它(指示false
以multipleInstances
防止它们)
OpenMDI<Form2>(false);