Chapter 24. Creating a GUI tool, part 2: the code

 

In the previous chapter, we created the graphical user interface for our PowerShell script; in this chapter, we’ll start adding the code needed to make that GUI functional. We’ll continue working in PowerShell Studio. But everything we’re doing will be much more code intensive, meaning you could choose to use the tool just to create the GUI, copy and paste the GUI-creation code into the PowerShell ISE, and then work in the ISE from there.

24.1. Addressing GUI objects

When we had PowerShell Studio create the script that implements the GUI, it assigned our GUI controls to variables. Those variables were named based on what we assigned to the Name property of our controls, so our Computername text box is in the $ComputerName variable. For controls we didn’t name explicitly, the tool made up a name—often based on the controls’ Text property, such as $LabelEnterComputerName. We always make a point to explicitly name any control we plan to work with or have users interact with, so that we can have a concise and sensible variable name.

We’ll use those variables to address and access the GUI controls, or objects. When followed by a period, they’ll let us access properties, such as $ComputerName.Text, which provides access to whatever was typed in the text box.

24.2. Example: text boxes

24.3. Example: button clicks

24.4. Example: list boxes

24.5. Example: radio buttons

24.6. Example: check boxes

24.7. Lab