C# 防止重复的 MDI 子表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1553363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent duplicate MDI children forms
提问by user
Is there a way to prevent the opening of a certain form within an MDI container if that said form is already opened?
如果该表单已经打开,是否有办法防止在 MDI 容器中打开某个表单?
采纳答案by Fredrik M?rk
You can interate over the OpenForms collection to check if there is already a form of the given type:
您可以对 OpenForms 集合进行交互以检查是否已经存在给定类型的表单:
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(MyFormType))
{
form.Activate();
return;
}
}
Form newForm = new MyFormType();
newForm.MdiParent = this;
newForm.Show();
回答by Vilx-
AFAIK there is no standard way. You'll have to implement it yourself. I'd do it this way:
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);
}
}
If thread safety is of concern, add the appropriate lock
s.
如果关注线程安全,请添加适当的lock
s。
回答by user3163424
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();
}
回答by Avijit
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();
回答by destinydz
This code work for me in vb.net
这段代码在 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()
回答by Dr Yunke
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# 和 VB.net 选项)实现方法,如果需要打开不同的 MDI 表单,这会很有用。
C#
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)
如下使用它(指出false
的multipleInstances
,以防止它们)
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)
按如下方式使用(指示False
为bMultipleInstances
防止它们)
Open_MDI(Of Form2)(False)
回答by mamal
This code work for me in C#
这段代码在 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();
}