如果该表格已经打开,是否有办法防止在MDI集装箱内打开某种形式?
防止重复的多指标类集调查儿童
原标题:Prevent duplicate MDI children forms
最佳回答
您可以相互交流,以检查是否有某种形式:
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(MyFormType))
{
form.Activate();
return;
}
}
Form newForm = new MyFormType();
newForm.MdiParent = this;
newForm.Show();
问题回答
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);
}
}
如果安全令人关切,则添加适当的<条码><>锁/代码>。
守则
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();
}
尽管这一职位非常老,但我认为这将增加帮助。
如果要尽量减少形式,则需要处理。 这里完全的例子就是:
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();
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()
一种方法可以采用通用方法(见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();
}
具体使用如下(指标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
以下列方式使用:
Open_MDI(Of Form2)(False)
该法典在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();
}
相关问题
热门标签
- winforms
- combobox
- fogbugz
- java
- date
- internationalization
- asp.net
- iis
- url-rewriting
- urlrewriter
- c#
- enums
- ocaml
- haxe
- algorithm
- string
- viewstate
- .net
- c++
- c
- symbol-table
- mysql
- database
- postgresql
- licensing
- migration
- vb.net
- vb6
- declaration
- vb6-migration
- python
- psycopg2
- backup
- vmware
- virtualization
- gnu-screen
- authentication
- desktop
- excel
- xll
- cultureinfo
- regioninfo
- oracle
- client
- session
- download
- html
- virtual
- constructor
- scenarios
- perl
- full-text-search
- javascript
- ajax
- testing
- oop
- inheritance
- vim
- encapsulation
- information-hiding