C# Question:Dynamically Disable ToolStripMenuItems/ToolStripButtons in an MDIParent Form: Which Event?
Any guidance on the following issue would be greatly appreciated. In which MDIParent event should I disable the items/buttons? Activated? On program Launch, I want the buttons disabled. If there are no active MDIChildren, I want the buttons disabled. When I launch a child form, I want to test that child form for d开发者_运维技巧ata. If it is a blank form, I want the buttons to remain disabled. I currently have my code in the MdiChildActivated Event Handler. Thanks for your time.
I used the Activate Event to disable all items/buttons. In the MDIChildActive Event I test for a blank form. If not blank, I enable the items/buttons.
I would use MDI Parent form's MdiChildActivate event: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.mdichildactivate.aspx
Please note this remark from that page:
You can use this event to perform tasks such as updating the contents of the MDI child form and changing the menu options available in the MDI parent form based on the status of the MDI child form that is activated.
Also note that this event is also called when a child is closed (from MSDN): Occurs when a multiple-document interface (MDI) child form is activated or closed within an MDI application.
That means that in this event you could do something like:
menuButton.Enabled = (this.MdiChildren.Length > 0);
or, if you need to check all child forms for some condition, and if one of children needs enabled button then enable the button:
void Form1_MdiChildActivate(object sender, EventArgs e)
{
foreach (Form child in MdiChildren)
{
if (IsToolbarButtonNeededForThisForm(child))
{
toolButton.Enabled = true;
break;
}
}
toolButton.Enabled = false;
精彩评论