在Mdi父窗体上,我使用菜单项调用我的子窗体。在子窗体上加载我的菜单项应该被禁用,在子窗体关闭时,它将再次启用。,我尝试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个ChildForms。通过使用这种方法,我的编码正在增加....,有其他方法可以替代....,
如果我理解对了的话:你有20个按钮,每个按钮打开一个特定的表单,对吗?
如果是,可以将每个按钮的tag-属性设置为它打开的表单。然后必须遍历所有按钮并设置click-event。所有按钮都有相同的点击事件。(我们把它命名为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;
}