【问题标题】:Disable Control From Other Form禁用其他表单的控制
【发布时间】:2013-07-27 22:00:15
【问题描述】:

我有一个打开 Form2 的 Form 1。如何使 Form2 中的所有 textBox 只读打开?

表格一:

Form2 f2 = new Form2();
f2.ReadOnly();
f2.ShowDialog();

表格2:

public void ReadOnyTextBoxes(Control parent)
{
    foreach (Control c in parent.Controls)
    {
        if (c.GetType() == typeof(TextBox))
        {
            ((TextBox)(c)).ReadOnly = true;
        }
    }
}

public void ReadOnly()
{
     ReadOnyTextBoxes(groupBox1);
}

【问题讨论】:

  • 您的实际问题是什么?您的代码似乎可以正常工作。
  • Form2 中的 textBoxes 仍然不是 ReadOnly

标签: c# winforms


【解决方案1】:

在 groupbox1 中可能有其他 groupbox 或一些容器。你需要递归。 How to disable all controls on the form except for a button?

【讨论】:

    【解决方案2】:

    使用您的想法,为了使其适用于所有TextBoxes,您可以使用递归函数来执行此操作,例如:

    public void MakeReadOnlyTextBoxes(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if (c.GetType() == typeof(TextBox))
            {
                ((TextBox)(c)).ReadOnly = true;
            }
            else if(c.Controls.Count > 0)
            {
                MakeReadOnlyTextBoxes(c);
            }
        }
    }
    
    public void ReadOnly()
    {
         ReadOnyTextBoxes(this);
    }
    

    已编辑:您应该在递归调用中使用 c 变量

    【讨论】:

      猜你喜欢
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 2011-08-30
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多