如何从表单中获取所有控件(包括任何容器中的控件)?

发布于 2024-08-07 11:28:38 字数 52 浏览 9 评论 0原文

例如,我需要一种方法来禁用表单中的所有按钮或验证所有文本框的数据。有什么想法吗?提前致谢!

I need, for example, a way to disable all buttons in a form or validate all textboxes' data. Any ideas? Thanks in advance!

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

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

发布评论

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

评论(4

写给空气的情书 2024-08-14 11:28:38

最简单的选择可能是级联:

public static void SetEnabled(Control control, bool enabled) {
    control.Enabled = enabled;
    foreach(Control child in control.Controls) {
        SetEnabled(child, enabled);
    }
}

或类似的;您当然可以传递一个委托以使其相当通用:

public static void ApplyAll(Control control, Action<Control> action) {
    action(control);
    foreach(Control child in control.Controls) {
        ApplyAll(child, action);
    }
}

然后是这样的:

ApplyAll(this, c => c.Validate());

ApplyAll(this, c => {c.Enabled = false; });

The simplest option may be to cascade:

public static void SetEnabled(Control control, bool enabled) {
    control.Enabled = enabled;
    foreach(Control child in control.Controls) {
        SetEnabled(child, enabled);
    }
}

or similar; you could of course pass a delegate to make it fairly generic:

public static void ApplyAll(Control control, Action<Control> action) {
    action(control);
    foreach(Control child in control.Controls) {
        ApplyAll(child, action);
    }
}

then things like:

ApplyAll(this, c => c.Validate());

ApplyAll(this, c => {c.Enabled = false; });
菩提树下叶撕阳。 2024-08-14 11:28:38

我更喜欢用惰性(迭代器)方法来解决问题,所以这就是我使用的方法:

/// <summary> Return all of the children in the hierarchy of the control. </summary>
/// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception>
/// <param name="control"> The control that serves as the root of the hierarchy. </param>
/// <param name="maxDepth"> (optional) The maximum number of levels to iterate.  Zero would be no
///  controls, 1 would be just the children of the control, 2 would include the children of the
///  children. </param>
/// <returns>
///  An enumerator that allows foreach to be used to process iterate all children in this
///  hierarchy.
/// </returns>
public static IEnumerable<Control> IterateAllChildren(this Control control,
                                                      int maxDepth = int.MaxValue)
{
  if (control == null)
    throw new ArgumentNullException("control");

  if (maxDepth == 0)
    return new Control[0];

  return IterateAllChildrenSafe(control, 1, maxDepth);
}


private static IEnumerable<Control> IterateAllChildrenSafe(Control rootControl,
                                                           int depth,
                                                           int maxDepth)
{
  foreach (Control control in rootControl.Controls)
  {
    yield return control;

    // only iterate children if we're not too far deep and if we 
    // actually have children
    if (depth >= maxDepth || control.Controls.Count == 0)
      continue;

    var children = IterateAllChildrenSafe(control, depth + 1, maxDepth);
    foreach (Control subChildControl in children)
    {
      yield return subChildControl;
    }
  }
}

I prefer a lazy (iterator) approach to the problem, so this is what I use:

/// <summary> Return all of the children in the hierarchy of the control. </summary>
/// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception>
/// <param name="control"> The control that serves as the root of the hierarchy. </param>
/// <param name="maxDepth"> (optional) The maximum number of levels to iterate.  Zero would be no
///  controls, 1 would be just the children of the control, 2 would include the children of the
///  children. </param>
/// <returns>
///  An enumerator that allows foreach to be used to process iterate all children in this
///  hierarchy.
/// </returns>
public static IEnumerable<Control> IterateAllChildren(this Control control,
                                                      int maxDepth = int.MaxValue)
{
  if (control == null)
    throw new ArgumentNullException("control");

  if (maxDepth == 0)
    return new Control[0];

  return IterateAllChildrenSafe(control, 1, maxDepth);
}


private static IEnumerable<Control> IterateAllChildrenSafe(Control rootControl,
                                                           int depth,
                                                           int maxDepth)
{
  foreach (Control control in rootControl.Controls)
  {
    yield return control;

    // only iterate children if we're not too far deep and if we 
    // actually have children
    if (depth >= maxDepth || control.Controls.Count == 0)
      continue;

    var children = IterateAllChildrenSafe(control, depth + 1, maxDepth);
    foreach (Control subChildControl in children)
    {
      yield return subChildControl;
    }
  }
}
沫尐诺 2024-08-14 11:28:38

还可以尝试:

public List<Control> getControls(string what, Control where)
    {
        List<Control> controles = new List<Control>();
        foreach (Control c in where.Controls)
        {
            if (c.GetType().Name == what)
            {
                controles.Add(c);
            }
            else if (c.Controls.Count > 0)
            {
                controles.AddRange(getControls(what, c));
            }
        }
        return controles;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var c = getControls("Button", this);

    }

Also try:

public List<Control> getControls(string what, Control where)
    {
        List<Control> controles = new List<Control>();
        foreach (Control c in where.Controls)
        {
            if (c.GetType().Name == what)
            {
                controles.Add(c);
            }
            else if (c.Controls.Count > 0)
            {
                controles.AddRange(getControls(what, c));
            }
        }
        return controles;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var c = getControls("Button", this);

    }
抱着落日 2024-08-14 11:28:38

我一直在寻找一种基于类型启用/禁用控件的解决方案,因此我想出了类似于 Luiscencio 的方法(您也可以修改它以获取所有控件或更改其他属性)。

public static void setEnabled (ControlCollection cntrList ,bool enabled,List<Type> typeList = null)
{
    foreach (Control cntr in cntrList)
    {
        if (cntr.Controls.Count == 0)
            if (typeList != null)
            {
                if (typeList.Contains(cntr.GetType()))
                    cntr.Enabled = enabled;
            }
             else
                cntr.Enabled = enabled;
        else
                setEnabled(cntr.Controls, enabled, typeList);
    }
}

public void loadFormEvents()
{
    List<Type> list = new List<Type> ();
    list.Add(typeof(TextBox));
    setEnabled(frm.Controls ,false,list);
}

I've been looking for a solution for the same to enable/disable controls based on type,so I came up with this similar to Luiscencio approach (You may also modify it to get all controls or change other properties).

public static void setEnabled (ControlCollection cntrList ,bool enabled,List<Type> typeList = null)
{
    foreach (Control cntr in cntrList)
    {
        if (cntr.Controls.Count == 0)
            if (typeList != null)
            {
                if (typeList.Contains(cntr.GetType()))
                    cntr.Enabled = enabled;
            }
             else
                cntr.Enabled = enabled;
        else
                setEnabled(cntr.Controls, enabled, typeList);
    }
}

public void loadFormEvents()
{
    List<Type> list = new List<Type> ();
    list.Add(typeof(TextBox));
    setEnabled(frm.Controls ,false,list);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文