【问题标题】:How can I better control behavior on multiple controls at once?如何更好地同时控制多个控件的行为?
【发布时间】:2011-10-12 17:27:21
【问题描述】:

所以我有一个表单,其中包含一些选项卡对象、组对象以及组对象内可能的表格布局面板。

基于数据锁定,表单将禁用其几乎所有内容,因此我需要将内容标记为只读,以便用户仍然可以看到数据但不能更改它...

这是我必须深入了解集合并将对象标记为只读的代码...

    private void lockForm()
    {
        btnLockData.BackColor = Color.LightBlue;
        btnLockData.Text = "Data locked!  Click to unlock...";

        foreach (Control formObject in this.Controls)
        {
            if (formObject is TabControl)
            {
                foreach (Control tabControl in (formObject.Controls))
                {
                    foreach (Control tabPageObject in (tabControl.Controls))
                    {
                        if (tabPageObject is TextBox)
                            ((TextBox)tabPageObject).ReadOnly = true;

                        if (tabPageObject is DataGridView)
                            ((DataGridView)tabPageObject).ReadOnly = true;


                        if (tabPageObject is GroupBox)
                        {
                            foreach (Control groupBoxObject in tabPageObject.Controls)
                            {
                                if (groupBoxObject is TextBox)
                                    ((TextBox)groupBoxObject).ReadOnly = true;

                                if (groupBoxObject is DataGridView)
                                    ((DataGridView)groupBoxObject).ReadOnly = true;

                                if (groupBoxObject is TableLayoutPanel)
                                {
                                    foreach (Control layoutObject in groupBoxObject.Controls)
                                    {
                                        if (layoutObject is TextBox)
                                            ((TextBox)layoutObject).ReadOnly = true;

                                        if (layoutObject is DataGridView)
                                            ((DataGridView)layoutObject).ReadOnly = true;                                            
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        dgvResources.ReadOnly = false;
        dgvDirectLabor.ReadOnly = false;
    }

有没有更好的方法来控制这种行为?

【问题讨论】:

  • foreach (Control c in this.Controls) { c.Enabled = false; } 或者更简单,myContainer.Enabled = false;

标签: c# winforms .net-3.5 controls


【解决方案1】:

要更精细地控制要设置只读的控件,请尝试:

   this.Controls.Cast<Control>()
        .Where(ctl => ctl is TextBox).Cast<TextBox>().ToList()
        .ForEach(e => e.ReadOnly = true);

    this.Controls.Cast<Control>()
        .Where(ctl => ctl is DataGridView).Cast<DataGridView>().ToList()
        .ForEach(e => e.ReadOnly = true);

【讨论】:

  • 就像此解决方案适用于每种类型的控件,以便我可以将其用于其他特定属性的情况
【解决方案2】:

这应该对 WinForms 有用:

var controls = from controls in this.Controls.OfType<Control>()
              select controls;
foreach(c in controls) { c.Enabled = false; }

【讨论】:

  • 提到 DataGridView 表明这是 Windows 窗体
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多