Вопрос

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

Это было полезно?

Решение

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

Другие советы

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.

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.

 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;
            }
        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top