Make all controls inside a GroupBox read-only?

三世轮回 提交于 2019-12-12 18:08:59

问题


I know that many individual controls have a ReadOnly property. But suppose I have a GroupBox in which there are many different controls (text boxes, combo boxes, radio buttons, etc ..), is it possible to set the ReadOnly property for all these controls together?

Not that I only want to set this property for the controls within a specific GroupBox (because I have multiple GroupBoxes too, so I don't want the setting to be done for controls in other GroupBoxes) ..

Manually setting the ReadOnly property seems very lethargic, as I have as many as 20 controls in each Groupbox (don't ask why :p).


回答1:


For standart controls in Winform you can use something like this (TextBoxBase is base class for ReadOnly properties controls) :

    private void button1_Click(object sender, EventArgs e)
    {
        SetReadonlyControls(groupBox1.Controls);
    }

    private void SetReadonlyControls(Control.ControlCollection controlCollection)
    {
        if (controlCollection == null)
        {
            return;
        }

        foreach (TextBoxBase c in controlCollection.OfType<TextBoxBase>())
        {
            c.ReadOnly = true;
        }
    }



回答2:


If disabled is enough you can disable the group box and the children will be disabled, too.

If you want to set inputs to read-only you need to iterate over the children, find out using reflection if they have an IsReadOnly property and then set it.




回答3:


Using Daniil's answer, since you mentioned there exists ComboBoxes and RadioButtons as well. This code :

TextBoxBase c in controlCollection.OfType<TextBoxBase>() 

wont be able to catch RadioButtons and ComboBoxes. You need to add following foreach loops as well.

private void button1_Click(object sender, EventArgs e)
    {
        SetReadonlyControls(groupBox1.Controls);
    }

    private void SetReadonlyControls(Control.ControlCollection controlCollection)
    {
        if (controlCollection == null)
        {
            return;
        }
        foreach (RadioButton r in controlCollection.OfType<RadioButton>())
        {
            r.Enabled = false; //RadioButtons do not have readonly property
        }
        foreach (ComboBox c in controlCollection.OfType<ComboBox>())
        {
            c.Enabled = false;//ComboBoxes do not have readonly property
        }
        foreach (TextBoxBase c in controlCollection.OfType<TextBoxBase>())
        {
            c.ReadOnly = true;
        }
    }

EDIT :

Make sure all the controls are inside groupbox1.




回答4:


 public void SetReadonlyControls(Control.ControlCollection controlCollection)
        {
            if (controlCollection == null)
            {
                return;
            }
            foreach (RadioButton r in controlCollection.OfType<RadioButton>())
            {
                r.Enabled = false; //RadioButtons do not have readonly property
            }
            foreach (ComboBox c in controlCollection.OfType<ComboBox>())
            {//AQUE
                var text = new TextBox();
                controlCollection.Add(text);
                text.Text = c.Text;
                text.Location = c.Location;
                text.Size = c.Size;
                text.Visible = true;`enter code here`
                c.Visible = false;
               /* c.Enabled = false;//ComboBoxes do not have readonly property
                c.ForeColor = System.Drawing.Color.White;
               c.DropDownStyle = ComboBoxStyle.Simple;*/
            }
            foreach (TextBoxBase c in controlCollection.OfType<TextBoxBase>())
            {
                c.ReadOnly = true;
            }
            foreach (DateTimePicker c in controlCollection.OfType<DateTimePicker>())
            {
                c.Enabled = false;
            }
        }


来源:https://stackoverflow.com/questions/15597271/make-all-controls-inside-a-groupbox-read-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!