简体   繁体   中英

Enabling / Disabling ToolStripMenuItem when clicked

I've two forms (called form1 as mdi container and form2) with an opentoolstripmenuitem in form1, when opentoolstripmenuitem clicked form2 called and opentoolstripmenuitem become disable, but when i click closebox in the top right of form2 the opentoolstripmenu still disable, i want it to enable again when closebox clicked.

Here my code in form1 :

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    openToolStripMenuItem.Enabled = false;
    Form2 newMDIChild = new Form2();
    newMDIChild.MdiParent = this;
    newMDIChild.Show();            
}

any advice??

In the click handler, create a handler for the Closing or Closed event for Form2:

newMDIChild.FormClosed += new FormClosedEventHandler(newMDIChild_FormClosed);

and

void newMDIChild_FormClosed(object sender, FormClosedEventArgs e)
{
        openToolStripMenuItem.Enabled = true;
}

Simple Just use ShowDialog() on form and enable it after Showdialog

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openToolStripMenuItem.Enabled = false;
Form2 newMDIChild = new Form2();
newMDIChild.MdiParent = this;
newMDIChild.ShowDialog(); 
openToolStripMenuItem.Enabled = true;           
}

The next code after showdialog will not be executed until the dialog is close

Hope it works

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM