我需要在VB.Net vs2008中打开一个MDI子实例...这段代码打开了同一个MDI子项的几个副本;对于c#我得到了这个答案Prevent duplicate MDI children forms但是没有找到VB.Net vs 2008的答案
Dim myChild As New Form1()
myChild.MdiParent = Me
myChild.Show()
答案 0 :(得分:4)
这是FredrikMö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()
答案 1 :(得分: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();
}
}
答案 2 :(得分:0)
可以使用Generics(在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);