Hey, Scripting Guy! I have really been enjoying reading your Weekend Scripter series. It seems like you guys really love scripting. I especially enjoyed the graphical trip cost calculator Ed wrote for the Scripting Wife.
My only problem with the article is that it was a bit quick, and I have never used Visual Studio or a Windows Form before. I am a complete beginner, and if it is not too much trouble, I could use a very basic example.
Microsoft Scripting Guy Ed Wilson here. I am listening to Vanessa-Mae, sipping a nice gunpowder green tea, and looking through pictures I took while scuba diving in Cozumel, Mexico. I have an old high school friend who is going down there, and I wanted to put a few pictures on Facebook for her to see. Here is one of my favorite photographs from Cozumel.
JB, the first thing you need to do after downloading the free PrimalForms Community Edition is open a new project. After the project appears, you need to modify the title of the Windows Form. To do this, change the Text property on the right side of the Primal Forms utility, as shown in the following image.
After you have modified the Text property of the new Windows Form, resize it. To do this, grab one of the three handles, to resize the width, the height, or both, as shown in the following image.
Using your left mouse button, drag a label control from the left palette to the Scripting Guys Clock form. When you move the mouse around inside the form, as you approach one of the four sides, little blue alignment lines will appear. Place the control somewhere in the middle of the form. Set the default text value for the label to Time, and rename the label from label1 to lbl_DisplayTime. The label name is in the Design section. This is shown in the following image.
Now we need to modify the size of the font. To do this, click the three dots next to the font property under appearance, and the Font tool shown in the following image appears. You can then choose the font, style, and size.
Drag a Button control to the Scripting Guys Clock form, and place it on the lower left side. Use the guidelines that appear to aid you in placement. After you have placed the button control, change the text of the button to Get Time as shown in the following image.
In the Design section, change the name of the button from button1 to btn_GetTime, as shown in the following image.
Now it is time to click Export PowerShell to save the Windows PowerShell code that will be used to create the Scripting Guys Clock form. This is shown in the following image. You may also wish to save the PrimalForms project, in case you ever want to modify the project in the future.
Open the clock project Windows PowerShell code in the Windows PowerShell ISE, and modify the code for the OnClick event. There are two comments in the code that will help you find this section. After you have located the code for the OnClick event, add code that will retrieve the current time, turn it into a string, and assign it to the Text property of the $lbl_DisplayTime object. This is seen here:
#Provide Custom Code for events specified in PrimalForms.
#TODO: Place custom script here
$lbl_DisplayTime.Text = (get-date).tostring()
When the Scripting Guys Clock runs, the form will appear. Click Get Time, and the value of the label changes from Time to the current date and time. Anytime you click Get Time, the time will be refreshed. This is shown in the following image.
JB, that is a more in-depth discussion of creating Windows Forms using Windows PowerShell and PrimalForms. The complete code for the Scripting Guys Clock is on the Script Center Script Repository. Graphical Windows PowerShell Week will continue tomorrow when we will talk about…wait a minute.
If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at email@example.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys