我正在使用菜单项调用我的子表单。在子窗体上加载我的菜单项应该禁用在子窗体关闭它将再次启用..,我尝试FormClosing事件处理程序我得到答案..,
private void btnMn1_Click(object sender, EventArgs e)
{
Forms.Cnblfrm cnbfrm = new Cnsmblfrm();
cnsmbfrm.MdiParent = this;
cnsmbfrm.Text = btnMn1.Text;
cnsmbfrm.Show();
this.btnMn1.Enabled = false;
cnbfrm.FormClosed += new FormClosedEventHandler(cnsmbfrm_FormClosed);
}
void cnbfrm_FormClosed(object sender, FormClosedEventArgs e)
{
btnMn1.Enabled = true;
//throw new NotImplementedException();
}
通过使用上面的代码,我得到了答案,但我有20多个ChildForm。通过使用这种方法我的编码正在增加....,有任何方法而不是这个....,
答案 0 :(得分:3)
如果我理解你的话:你有20个按钮,每个按钮打开一个特定的表格,对吗?
如果是这样,您可以将每个按钮的tag-property设置为它打开的表单。然后你必须遍历所有按钮并设置click事件。所有按钮都具有相同的点击事件。 (我们称之为btn_click)
btn_click的代码可能如下所示:
private void btn_click(object sender, EventArgs e)
{
Button button = sender as Button;
if(button == null)
return;
Form form = button.Tag as Form;
if(form == null)
return;
form.MdiParent = this;
form.Text = button.Text;
form.Show();
button.Enabled = false;
form.Tag = button;
form.FormClosed += FormClosed;
}
private void FormClosed(object sender, FormClosedEventArgs e)
{
Form form = sender as Form;
if(form == null)
return;
Button button = form.Tag as Button;
if(button == null)
return;
button.Enabled = true;
}