How to enable ALL controls on the form?

Currently, I have most form controls disabled at startup because you cannot use them until the file is uploaded. However, after loading the file, the controls must be enabled.

I used bindings, but I don't think they are a good solution. Firstly, it’s unnecessary complexity. Secondly, you cannot use bindings for everything. For example, MenuStrip items cannot have their Enabled object attached to the fileLoaded property. Only all menus can and I don’t want to disable all menus at startup, only certain menu operations that work with the file.

I am really looking for a way to include EVERYTHING. Most, when asked, will answer with this:

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

However, this does not work to include MenuStrip elements or controls in other controls (such as a panel or user control). Therefore, it would not include scrollbars in containers.

I suppose I could use this line and manually include everything else, but I could always just include everything manually. I am looking for a way to automatically enable everything.

+5
source share
5 answers

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

Had to check collection control counter instead of HasControls, which is Webcontrols

+12
source

Place all controls in a panel;

panel.enable = false → panel.enable = true → ( , , , . )

+6

menustrip , . item.Enabled true false.

, , . , . :

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
    }
}

:

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

:

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

Enable(YourForm);
+2

. , - , VS Designer. , ! ( ) .

If you still want to go to a recursive route, I would change the Enable method, first renaming it to ChangeEnabledState, and secondly, I would use the bool parameter, which allows you to assign a property Enabled. This way you can enable / disable controls as needed. However, remember that you will need to add a check to see if the control button (or anything you use to open OpenFileDialog) is pressed in the shutdown operation.

+1
source

All Articles