In my previous article in this series, I showed you how to output a list of virtual machine names in response to a button click. Doing so proves that a form can display dynamic data, but we still aren’t doing anything that is all that useful. I still plan to show you how to build a GUI based application that provides useful information, but before I do, I want to show you a few techniques for adding a bit of visual flair to the interface.

Let’s start out by displaying the output in a text box. As you may recall from the previous article, the form’s output is typed directly onto the form’s background. You can see what that looks like in Figure A. Technically there is nothing wrong with doing that, but we can display the output in other ways.

Figure A: The form’s output is written in the middle of the dialog box.

Displaying the list of virtual machines in a text box is surprisingly easy to do. In fact, you can do it by changing a single word. Take a look at the DisplayVMs function that causes the list of virtual machines to be displayed in response to a button click:

#Define Button Click Function

Function DisplayVMs

{

$VirtualMachines = Get-VM

Add-Type -AssemblyName System.Windows.Forms

#Create text

$Label = New-Object System.Windows.Forms.textbox

$Label.AutoSize = $True

Foreach ($vm in $VirtualMachines){

$NL = "`n"

$Label.Text = $Label.Text + $VM.Name + $NL

}

$Label.Location = new-object System.Drawing.Size(200,110)

$Form.Controls.Add($Label)

}

The script contains a line that reads:

$Label = New-Object System.Windows.Forms.textbox

This line previously read:

$Label = New-Object System.Windows.Forms.label

By changing the word Label to Textbox, I was able to change the way that the output was displayed. You can see what this looks like in Figure B.

Figure B: The output is now displayed in a text box.

As you look at the figure above, you will notice that although the output has been displayed in a text box, there are some formatting problems. Specifically, the text box is way too small, and all of the output has been jammed together on a single line. Fortunately, this is easy enough to fix.

To display the text on multiple lines, there are two lines of code that need to be added. These lines are:

$Label.Size = New-Object System.Drawing.Size(150,150)

$Label.MultiLine = $True

The first of these lines defines the size of the text box, while the second line of code allows the textbox to display multiple lines of output. You can see the result in Figure C. It still isn’t perfect, but we are getting closer.

Figure C: The text box now displays multiple lines of text.

The problem that we are running into now is that some of the virtual machine names are still jammed together. Thankfully, this too is an easy fix. You might recall that when we were displaying the output as a label, we had to insert `n after each virtual machine name, in order to start a new line. This doesn’t quite work when we switch from a label to a text box. The fix is to use `r`n instead of `n. This code is used to tell PowerShell that we want a carriage return and a new line. Here is the actual modification to the script:

$NL = "`r`n"

You can see the resulting output in Figure D.

Figure D: We have fixed the line item problem.

Now, I want to show you how to add a bit of visual flair to the form that you have created. One way of doing so is to add a splash of color. How about we start out by changing the form’s background color?

As you may recall, I used a variable named $Form to define the characteristics of the form. If we want to add a background color to the form, we can do so by adding a line of code that sets the $Form.BackColor. For example, I could make the form’s background light blue by adding this line of code:

$Form.BackColor = "lightblue"

You can see the results in Figure E.

Figure E: The form’s background is now light blue.

OK, that’s great, but now the button kind of disappears into the background. So what can we do about changing the color of the button? Well, we can use exactly the same technique, except that this time the background color needs to be applied to the button instead of to the form as a whole. If for example, you wanted to make the button light gray, you could add this line of code:

$Button1.BackColor ="LightGray"

You can see the results in Figure F.

Figure F: The button is now light gray.

Personally, I think that the form looks fine now, but let’s suppose that we wanted to change the color of the “click the button” message. In order to do so, you have to set the foreground color of the label object. If for example, you wanted to color the label text to be dark blue, you could use this line of code:

$Label2.ForeColor = "DarkBlue"

You can see what this looks like in Figure G.

Figure G: I have changed the text color to dark blue.

I realize that I have made a lot of changes to the script, so here is the complete code as it currently exists (a few lines were rearranged):

Featured Links

Read Next

Brien Posey

Brien Posey is a freelance technology author and speaker with over two decades of IT experience. Prior to going freelance, Brien was a CIO for a national chain of hospitals and healthcare facilities. He has also served as a network engineer for the United States Department of Defense at Fort Knox. In addition, Brien has worked as a network administrator for some of the largest insurance companies in America. To date, Brien has received Microsoft’s MVP award numerous times in categories including Windows Server, IIS, Exchange Server, and File Systems / Storage. You can visit Brien’s Website at: www.brienposey.com.

This can be done, but I don't know of an easy way of doing it. You have to insert a new line code into the text string in order to get the text to be displayed on a new line. If the text is coming from a text file, then this means writing code to count characters, look for spaces, and insert new line codes in appropriate places. Maybe I can write the code for that in a future article.

Latest Podcast

Featured Freeware

Recommended

Follow Us

Building a PowerShell GUI (Part 8)

TECHGENIX

TechGenix reaches millions of IT Professionals every month, and has set the standard for providing free technical content through its growing family of websites, empowering them with the answers and tools that are needed to set up, configure, maintain and enhance their networks.