PowerShell Studio: Enable and Disable Groups of Controls
- Details
- Written by David Corrales
- Last Updated: 14 April 2016
- Created: 28 July 2013
- Hits: 19700
The Enable property is shared by all WinForms controls. This property allows you to disable controls so they no longer can be interacted with.
For example, after pressing a button that triggers a Job, it is necessary to disable the button in order to prevent the user from pressing it again until the Job completes. Some instances might require you to disable a group of controls. In this case, your code may look like the following:
$buttonAdd.Enable = $false $buttonRemove.Enable = $false $comboBoxOptions.Enable = $false
This list of controls can get rather long, especially when working with a large group of controls.
The good news is WinForms provides an easy alternative for disabling a group of controls and this can be accomplished by using container controls. A container control is a control that allows you to place other controls within it. Examples of container controls include:
GroupBox
Panel
TabControl
TableLayoutPanel
Once you have decided which container control to use, simply place all the controls you wish to enable / disable within the container. In this case I used a GroupBox to group my controls:
Now when you need to disable the controls, you simply set the GroupBox’s Enable property to False and all the controls contained within will be disabled.
$buttonDisableGroup_Click={ $groupbox1.Enabled = $false }
WARNING: It is not recommended using the Form’s Enable property. Doing so will result in an unresponsive window and will prevent you from interacting with the GUI. In other words, you will not be able to move, resize, or exit the form.
You can download the Enable and Disable Controls Sample here.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.