我是编程的初学者。我尝试用多种形式创建小应用程序。我想解释一下,如何使用using方法创建此表单的实例来打开表单。
我实际上是这样的:
private void firtsToolStripMenuItem_Click(object sender, EventArgs e)
{
if(myForm == null)
{
myForm = new MyForm();
myForm.MdiParent = this;
myForm.FormClosing += myFormForm_FormClosing;
myForm.Show();
}
else
{
myForm.Activate();
}
}
void myForm_FormClosing(object sender, FormClosingEventArgs e)
{
myForm = null;
}
我想处理很多工具条菜单,我不想在每个菜单中使用该代码,因此我想编写一些方法来打开任何其他类型的表单。
答案 0 :(得分:0)
创建一个方法,如果传递的形式尚未实例化,将创建新实例。
private ShowForm<T>(T form) where T : Form
{
if(form == null)
{
form = Activator.CreateInstance(typeof(T));
form.MdiParent = this;
form.FormClosing += myFormForm_FormClosing;
form.Show();
}
else
{
form.Activate();
}
}
答案 1 :(得分:0)
这允许您最多显示每个类的一个MDI子表单:
pp=str(input())
p1=float(pp[:pp.find(' ')])
p2=float(pp[pp.find(' ')+1:])
pf=0.0
pa=0.0
from math import factorial as f
def comb(n,r):
return f(n) / f(r) / f(n-r)
for i1 in range(0,7):
if i1>=3:
pf=pf+(pow(p1,i1)*pow(p2,6-i1))*comb(6,i1)
pa=pa+(pow(p1,i1)*pow(p2,6-i1))*comb(6,i1)
print("%.3f"%(pf/pa))
用法:
// Stores references to form of each type:
private Dictionary<Type, Form> _childWindows = new Dictionary<Type, Form>();
private Form ShowForm<T>() where T : Form
{
var formType = typeof(T);
// If we already display a form of given type -> activate it
if (_childWindows.ContainsKey(formType))
{
var form = _childWindows[formType];
form.Activate();
return form;
}
else
{
// Else create a new form instance
var form = (Form) Activator.CreateInstance(typeof(T));
form.MdiParent = this;
form.FormClosing += myFormForm_FormClosing;
_childWindows[formType] = form;
form.Show();
return form;
}
}
void myForm_FormClosing(object sender, FormClosingEventArgs e)
{
_childWindows.Remove(sender.GetType())
}