如何启用表单上的所有控件?

发布于 2024-12-01 09:55:00 字数 482 浏览 1 评论 0原文

目前,我在启动时禁用了表单的大部分控件,因为在加载文件之前您无法使用它们。然而,一旦文件被加载,控件就应该被启用。

我正在使用绑定,但我认为它们不是一个好的解决方案。其一,这是不必要的复杂性。其次,你不能对所有事情都使用绑定。例如,MenuStrip 项不能将其 Enabled 属性绑定到 fileLoaded 属性。只有整个菜单可以,而且我不想在启动时禁用整个菜单,只禁用对文件进行操作的某些菜单操作。

我真的只是在寻找一种方法来实现一切。大多数人在被问到时会这样回答:

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

但是,这不适用于在其他控件(如面板或自定义控件)中启用 MenuStrip 项或控件。因此它不会在容器内启用滚动条。

我想我可以使用该行并手动启用其他所有内容,但我可以始终手动启用所有内容。我正在寻找一种自动启用一切的方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

神经大条 2024-12-08 09:55:00

递归

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);

    }
}

编辑

应该检查控件集合计数而不是HasControls,这是Webcontrols

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

甜味拾荒者 2024-12-08 09:55:00

将所有控件放在一个面板中;

panel.enable = false ->;面板中的所有控件将被禁用
panel.enable = true ->;面板中的所有控件都将被启用(如果它们默认启用,请立即用启用的控件填充您的面板,让您的面板禁用,以便您的控件也将被禁用。启用面板后,您的控件将被启用)

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 )

好多鱼好多余 2024-12-08 09:55:00

对于菜单条项目,您可以递归地执行,这非常简单。
只需将 item.Enabled Flag 设置为 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;
}

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;
}
叹梦 2024-12-08 09:55:00

这样做:

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

Enable(YourForm);

Do this:

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

Enable(YourForm);
感情洁癖 2024-12-08 09:55:00

要禁用菜单中的每个项目,必须递归迭代菜单。然而,对于所有表单控件,有一个更简单的解决方案 - 将它们嵌入到跨越整个表单的面板中,并在 VS 设计器中禁用该表单。当您的用户选择文件时,启用面板和中提琴!不需要额外的递归(或开销)。

如果您仍然想采用递归路线,我将首先将其重命名为 ChangeEnabledState 来更改 Enable 方法,其次,我将使用一个 bool 参数来分配给 已启用 属性。这样您就可以根据需要启用/禁用控件。但请记住,您需要添加一个检查来查看该控件是否是在禁用操作中跳过的按钮(或者您用于打开 OpenFileDialog 的任何控件)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文