John Smith
John Smith

Reputation: 8821

How to enable ALL controls on a form?

Currently I have most of my form's controls disabled at launch because you cannot use them until a file is loaded. However, once the file is loaded the controls should become enabled.

I was using bindings but I don't think they're a good solution. For one, it is unnecessary complexity. Secondly, you cannot use bindings for everything. For example, MenuStrip items cannot have their Enabled property bound to the fileLoaded property. Only the entire menu can and I don't want to disable the entire menu at launch, only certain menu operations that operate on the file.

I'm really just looking for a way to enable EVERYTHING. Most when asked that would answer with this:

foreach (Control c in Controls)
{
    c.Enabled = true;
}

However, that does not work for enabling MenuStrip items or controls within other controls (like a Panel or custom control). Therefore it would not enable scrollbars within containers.

I suppose I could use that line and manually enable everything else but I could have always just manually enabled everything. I'm looking for a way to automatically enable everything.

Upvotes: 6

Views: 15597

Answers (5)

Stefan0410
Stefan0410

Reputation: 121

For menustrip items you can do it recursively, it's very simple. Just set the item.Enabled Flag to true or false.

You can use my class, it's just a few lines code, easy to use. You can also create a Search Function and just pass the name want to disable. Enjoy:

namespace Test_MenuItemIteration
{
    using System.Collections.Generic;
    using System.Windows.Forms;

    public static class GetAllMenuStripItems
    {
        #region Methods

        /// <summary>
        /// Gets a list of all ToolStripMenuItems
        /// </summary>
        /// <param name="menuStrip">The menu strip control</param>
        /// <returns>List of all ToolStripMenuItems</returns>
        public static List<ToolStripMenuItem> GetItems(MenuStrip menuStrip)
        {
            List<ToolStripMenuItem> myItems = new List<ToolStripMenuItem>();
            foreach (ToolStripMenuItem i in menuStrip.Items)
            {
                GetMenuItems(i, myItems);
            }
            return myItems;
        }

        /// <summary>
        /// Gets the menu items.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="items">The items.</param>
        private static void GetMenuItems(ToolStripMenuItem item, List<ToolStripMenuItem> items)
        {
            items.Add(item);
            foreach (ToolStripItem i in item.DropDownItems)
            {
                if (i is ToolStripMenuItem)
                {
                    GetMenuItems((ToolStripMenuItem)i, items);
                }
            }
        }

        #endregion Methods
    }
}

Use in your app:

List<ToolStripMenuItem> myItems = GetAllMenuStripItems.GetItems(this.menuStrip1);
foreach (var item in myItems)
{
    MessageBox.Show(item.Text);
    item.Enabled = false;
}

Upvotes: 3

icaptan
icaptan

Reputation: 1535

Put all controls in a panel;

panel.enable = false -> all controls in the panel will be disabled panel.enable = true -> all controls in the panel will be enabled (if they are in default enabled, shortly fill your panel with enabled controls, let your panel disabled so that your controls will be disabled, too. After enabling panel, your controls will be enabled )

Upvotes: 6

RLH
RLH

Reputation: 15698

Recursively iterating over your menu is necessary to Disable every item in your menus. However, there is a simpler solution for all of your form controls-- embed them in a panel that spans across the entire form and disable the form in the VS Designer. When your user selects a file, enable the Panel and viola! No extra recursion (or overhead) necessary.

If you still want to go the recursive route, I would change the Enable method by first renaming it to ChangeEnabledState and, second, I would a a bool parameter that would allow you use to assign to the Enabled property. That way you can enable/disable the controls as necessary. Remember, however, you will need to add a check to see if the control is the button (or what ever control you are using to open the OpenFileDialog) is skipped in a disable operation.

Upvotes: 1

msarchet
msarchet

Reputation: 15242

Recursion

private void enableControls(Control.ControlCollection Controls)
{
    foreach (Control c in Controls)
    {
        c.Enabled = true;
        if (c is MenuStrip)
        {
           foreach(var item in ((MenuStrip)c).Items)
           { 
              item.Enabled = true;
           }
        }
        if (c.ControlCollection.Count > 0)
            enableControls(c.Controls);

    }
}

Edit

Should have been checking the control Collection count instead of HasControls Which is Webcontrols

Upvotes: 12

Daniel
Daniel

Reputation: 31579

Do this:

var Enable = (Control c) =>
             {
                 c.Enabled = true;
                 if(c.Controls != null)
                     foreach(Control c2 in c.Controls)
                         Enable(c2);
             }

Enable(YourForm);

Upvotes: 2

Related Questions