gpt4 book ai didi

c# - 用户不耐烦多次点击 menuitem 多次加载 MDI 子窗体

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:53 26 4
gpt4 key购买 nike

有些表单我只想由 MDI Parent 加载一次。因此,在菜单项的单击事件中,我查看 MDI 父级的 .Children 属性是否已包含该表单,并且仅当它不在子项中时才加载该表单:

       bool alreadyLoaded = false;
if (this.MdiChildren.Length > 0)
{
foreach (Form frm in this.MdiChildren)
{
if (frm.Name == "foo")
{
alreadyLoaded=true;
break;
}
}
}


if (! alreadyLoaded)
{
FOO f = new FOO();
f.MdiParent=this;
f.Show();
}

但是如果不耐烦的用户在第一次加载表单时快速单击菜单项,则可能会加载多次。在第一次单击加载的实例完全实例化之前,第二次和第三次单击菜单项会启动表单的新副本。

我想我可以维护自己的已启动表单名称列表,并在菜单项的单击事件处理程序中,将表单名称添加到列表中(如果不存在),然后将其从子表单的 Closed 列表中删除事件处理程序。但是有更好的方法吗?

最佳答案

试试这门课:

/// <summary>
/// Displays form in mdi form once
/// </summary>
/// <autor>Saber Amani</autor>
/// <lastUpdate>2009-03-03</lastUpdate>
public class MdiFormDisplayer
{
private Hashtable fForms = new Hashtable();
private object fSender = null;

public MdiFormDisplayer(object sender)
{
fSender = sender;
}

public Form GetForm(Type formType)
{
string formName = formType.Name;
Form frm = (Form)fForms[formName];
if (frm == null || frm.IsDisposed)
{
frm = CreateNewInstance(formType);
fForms[formName] = frm;
}
return frm;
}

public Form GetForm(string formName)
{
if (fSender == null)
throw new ArgumentNullException("Sender", "Sender can't be null");
return GetForm(fSender, formName);
}

public Form GetForm(object sender, string formName)
{
Form frm = (Form)fForms[formName];
if (frm == null || frm.IsDisposed)
{
frm = CreateNewInstance(sender, formName);
fForms[formName] = frm;
}
return frm;
}

private Form CreateNewInstance(object sender, string formName)
{
Type frmType;
frmType = FindFormType(sender, formName);
Form frmInstance = (Form)CallTypeConstructor(frmType);

return frmInstance;
}

private Form CreateNewInstance(Type frmType)
{
Form frmInstance = (Form)CallTypeConstructor(frmType);
return frmInstance;
}

private Type FindFormType(object sender, string formName)
{
Type baseType = sender.GetType();
Assembly senderAssembly = Assembly.GetAssembly(baseType);

Type result = null;

// Search with assembly standard method
result = senderAssembly.GetType(baseType.Namespace + "." + formName);
if (result != null)
return result;

// Search with in the types
Type[] assemblyTypes = senderAssembly.GetTypes();
formName = formName.ToLower();
for (int i = 0; i < assemblyTypes.Length; i++)
{
if (assemblyTypes[i].Name.ToLower() == formName)
return assemblyTypes[i];
}

return null;
}

private object CallTypeConstructor(Type frmType)
{
Type[] contructTypes = new Type[] { };
ConstructorInfo constructorObj = frmType.GetConstructor(contructTypes);
object result = constructorObj.Invoke(null);
return result;
}


}

像这样使用:

    public partial class frmMainForm : Form
{
MdiFormDisplayer forms;

public frmMainForm()
{
InitializeComponent();
forms = new MdiFormDisplayer(this);
}

private void btnCompany_Click(object sender, EventArgs e)
{
Form frm = forms.GetForm(typeof(frmCompany));

frm.MdiParent = this;
frm.Show();
frm.Focus();
}

}

希望这对您有所帮助。

关于c# - 用户不耐烦多次点击 menuitem 多次加载 MDI 子窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6819387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com