0

is there a way where I can disable all texboxes, datetimepicker, comboboxes in a form load?

I have a condition in my form_load like this:

    private void frmTWLeasing_Load(object sender, EventArgs e)
        {
            //this.Text = sTitle;
            this.txtNinja1.Text = sEnable;
            if (sEnable == "Disabled")
            {

                disable all textboxes, datetimepicker, comboboxes or gridviews
            }
            else
            {
               enable all textboxes, datetimepicker, comboboxes or gridviews
            }
        }

I have so many textboxes, datetimepicker, and comboboxes in this form including a datagridview, so is there any syntax where I can call all textboxes, datetimepicker, comboboxes so that I can disable this at one time? It's hard to disable it one by one.. Hope you can help me guys Thanks in advance!!

eljon_i3
  • 169
  • 2
  • 4
  • 24
  • It would be easier to put the ones you want to disable into a Panel and then just disable that. – cbr Mar 11 '15 at 06:27

1 Answers1

1

You can use OfType() and then in Where() get specific type control and then iterate and disable them or enable them as desired like this:

 if (sEnable == "Disabled")
 {

    var controls = this.Controls.OfType<Control>().Where(x=>x is TextBox || x is ComboBox || x is DateTimePicker);

    foreach(var control in controls)
          control.Enabled = false;
 }

You will need to add using System.Linq in your class

If you have controls in nested controls, means inside some user control or some other control, then you need to check recursively as in this SO post (How to get ALL child controls of a Windows Forms form of a specific type)

Community
  • 1
  • 1
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • I've tried it, but i'm getting an error on OfType – eljon_i3 Mar 11 '15 at 05:42
  • Note that both cases are handled (enable and disable) if one simply uses `control.Enabled = sEnable != "Disabled"`. Also, the above will only handle the first level of controls. A recursive implementation should be used if any of the targeted controls are sub-children within the form. – Peter Duniho Mar 11 '15 at 05:43
  • There is a typo. Ehsan meant to write `this.Controls.OfType()`...note the parentheses. – Peter Duniho Mar 11 '15 at 05:44
  • @PeterDuniho totally agree, i have just gave idea to OP how to get started – Ehsan Sajjad Mar 11 '15 at 05:44
  • @EhsanSajjad Right, I did add that, however it didn't work.. Nothing happened. – eljon_i3 Mar 11 '15 at 05:48
  • you have controls in form or you have a user control inside form and conrols in it? – Ehsan Sajjad Mar 11 '15 at 05:57
  • @EhsanSajjad I have controls in my form, this is just a form load, i don't have any codes in my form rather than in my form_load.. – eljon_i3 Mar 11 '15 at 06:49
  • @EhsanSajjad And by the way my textboxes, datetimepickers and comboboxes are in a Tab Control Panel, will this affect that code? – eljon_i3 Mar 11 '15 at 06:52
  • @eljon_i3 then you have to do recursivley see the link i posted in my answer , the above code will work if controls are directly in form – Ehsan Sajjad Mar 11 '15 at 06:56
  • @EhsanSajjad I don't get how can I do it recursively, but I guess the link would help me.. Can you have an example maybe? – eljon_i3 Mar 11 '15 at 08:35
  • @eljon_i3 it was wrong link connected check now this :http://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-windows-forms-form-of-a-specific-type-button – Ehsan Sajjad Mar 11 '15 at 09:06