How to enable ALL controls on a form?

前端 未结 5 585
清歌不尽
清歌不尽 2021-01-03 00:08

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 beco

5条回答
  •  暖寄归人
    2021-01-03 00:34

    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
    
            /// 
            /// Gets a list of all ToolStripMenuItems
            /// 
            /// The menu strip control
            /// List of all ToolStripMenuItems
            public static List GetItems(MenuStrip menuStrip)
            {
                List myItems = new List();
                foreach (ToolStripMenuItem i in menuStrip.Items)
                {
                    GetMenuItems(i, myItems);
                }
                return myItems;
            }
    
            /// 
            /// Gets the menu items.
            /// 
            /// The item.
            /// The items.
            private static void GetMenuItems(ToolStripMenuItem item, List 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 myItems = GetAllMenuStripItems.GetItems(this.menuStrip1);
    foreach (var item in myItems)
    {
        MessageBox.Show(item.Text);
        item.Enabled = false;
    }
    

提交回复
热议问题