Question

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.

Was it helpful?

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)

OTHER TIPS

I like Dominic's solution. Another approach (and this depends on how your Winform is laid out) would be to group the controls into a panel:

For Each ctrl as Control in MyPanel.Controls
    c.Visible = False
Next

Really all this approach does is keeps you from having to create a new list, but maybe that would be better so you can choose precisely which controls to add.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top