PowerShell Studio: Initializing my GUI controls

August 5th, 2014 by David Corrales
Last updated on August 5th, 2014

 

If you are working with a GUI script within PowerShell Studio and find that you need to edit the exported script to modify the control’s initialization, then you are doing something wrong.

Don’t get me wrong, modifying and initialization of control properties is not the issue; in fact that it part of writing a GUI. What I’m referring to is the part where you are editing the exported scripts.

If you are editing the exported script:

1. You are creating an extra step which means you need to export and modify the script anytime you make a change to the original file.

2. It is time consuming and unproductive.

3. If you are working with Source Control or within a team environment, chances are the change will be missed or forgotten by somebody.

 

So if I’m not supposed to edit the exported script, then how do I initialize my controls?

Thankfully WinForms provides a convenient place to initialize your GUI controls, the Form control’s Load event.

The Form control’s Load event is called right before the form is displayed, which makes it a perfect place to initialize your controls.

$formServices_Load={
    #TODO: Initialize Form Controls here
    #Load the services into the the combobox
Load-ComboBox -ComboBox $comboboxServices -Items (Get-Service) -DisplayMember Name #Display the computer name in the form's title
$formServices.Text ="Services on $env:ComputerName" }

 

Warning: Do not try to initialize a control outside an event block within the script.

1. The control might not exist when this line is executed by PowerShell.

2. If the control does exist, your changes will most likely get overwritten by the designer generated script.

Alternatives to the Load event:

It is possible to use other events to trigger initialization. For example, some control sets such as the Chart – Disk Space use the VisibleChanged event to trigger initialization. This event is triggered when the control is displayed (such as when the form is loaded) or hidden using the Visible property.

$chartDiskSpace_VisibleChanged={
    if($this.Visible)
    {
        Load-DiskChart$this
    }
}

In the sample above, the control set initializes when the control becomes visible by checking its Visible property.

 

If your initialization script is running slow it can prevent the GUI from displaying in a timely manner or cause it to hang. In instances such as this, you might want to delay your initialization or simply run the query in a separate job then initialize and enable the controls after the job has completed the query.

Warning: You cannot access GUI controls directly from a Job. You must return the results first and then initialize the controls on the main thread / script.

For more details on how to use Jobs within a GUI script, please refer to the Creating Responsive Forms article.

For more details on Form control, please refer to the Spotlight on the Form Control article.

 

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

Tags: , ,

Comments are closed.