Monday, 10 July 2017

Control Enable ReadOnly Method based on value or text

Control Enable ReadOnly Method based on value or text

private void button1_Click(object sender, EventArgs e)
        {
            List<Control> Controls = new List<Control>()
            {
                {textBox1},
                {comboBox1},
                {dateTimePicker1}
            };
            ControlEnableReadOnlyMethod(Controls);
        }


private void ControlEnableReadOnlyMethod(List<Control> formControl)
        {
            foreach (Control control in formControl)
            {
                if (control.GetType() == typeof(TextBox))
                {
                    if (((TextBox)(control)).Text != string.Empty)
                        ((TextBox)(control)).ReadOnly = true;
                    else
                        ((TextBox)(control)).ReadOnly = false;
                }
                if (control.GetType() == typeof(ComboBox))
                {
                    if (((ComboBox)(control)).Text != string.Empty)
                        ((ComboBox)(control)).Enabled = false;
                    else
                        ((ComboBox)(control)).Enabled = true;
                }
                if (control.GetType() == typeof(DateTimePicker))
                {
                    if (((DateTimePicker)(control)).Value == null)
                        ((DateTimePicker)(control)).Enabled = true;
                    else
                        ((DateTimePicker)(control)).Enabled = false;
                }
            }
        }