PowerShell Studio: Enable and Disable Groups of Controls

February 12th, 2013 by David Corrales
Last updated on February 12th, 2013

 

If you follow the Spotlight on Controls series you might be familiar with the Enable property that 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:

GroupBox of 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
}

Disabled Groupbox

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 from our Downloads section under Sample Scripts.

 

 
[Google+]   [Facebook]   [LinkedIn]   [StumbleUpon]   [Digg]   [Reddit]   [Google Bookmark]  

Tags: , , , ,

Comments are closed.