0

在 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。通过使用这种方法我的编码正在增加......,有什么方法可以代替这个......,

4

1 回答 1

3

如果我理解正确:您有 20 个按钮,每个按钮都打开一个特定的表单,对吗?

如果是这样,您可以将每个按钮的标签属性设置为它打开的表单。然后你必须遍历所有按钮并设置点击事件。所有按钮都有相同的点击事件。(我们称之为 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;
}
于 2013-03-25T12:16:34.107 回答