Search code examples
vb.netwinformscontrolsvisible

Set a group of controls visible with one line of code?


Is it possible to clump a group of controls together and be able to set it visible with one line rather than having to do each individual control's .visible property? I know it doesn't hurt anything but would like to keep it looking neat and not clump up a function with a page full of .visible control calls.


Solution

  • Just group your controls in a List(Of Control) or an array and set the Visible property using either the ForEach-method or a simple For Each-loop.

    e.g.:

    Dim toToggle = {OkButton, CancelButton, ControlPanel, SelectionComboBox}
    For Each ctrl in toToggle
        ctrl.Visible = False
    Next
    

    or

    Dim toToggle = {OkButton, CancelButton, ControlPanel}.ToList()
    toToggle.ForEach(Sub(c) c.Visible = False)