{"id":51143,"date":"2010-03-01T00:01:00","date_gmt":"2010-03-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/03\/01\/hey-scripting-guy-how-can-i-create-a-windows-form-using-windows-powershell-and-primalforms\/"},"modified":"2010-03-01T00:01:00","modified_gmt":"2010-03-01T00:01:00","slug":"hey-scripting-guy-how-can-i-create-a-windows-form-using-windows-powershell-and-primalforms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-create-a-windows-form-using-windows-powershell-and-primalforms\/","title":{"rendered":"Hey, Scripting Guy! How Can I Create a Windows Form Using Windows PowerShell and PrimalForms?"},"content":{"rendered":"<p><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" width=\"125\" height=\"16\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\"><\/a><\/p>\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" width=\"34\" height=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\">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 <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/02\/27\/hey-scripting-guy-february-27-2010.aspx\">graphical trip cost calculator<\/a> Ed wrote for the Scripting Wife.<\/p>\n<p class=\"MsoNormal\">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. <\/p>\n<p class=\"MsoNormal\">&#8212; JB<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" width=\"34\" height=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\">Hello JB, <\/p>\n<p class=\"MsoNormal\">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.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Photograph taken in waters off Cozumel, Mexico\" alt=\"Photograph taken in waters off Cozumel, Mexico\" width=\"600\" height=\"431\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0301\/hsg-03-01-10-01.jpg\"><\/p>\n<p class=\"Fig-Graphic\">JB, the first thing you need to do after downloading the free <a href=\"http:\/\/www.primaltools.com\/downloads\/communitytools\/\"><font face=\"Segoe\">PrimalForms Community Edition<\/font><\/a> is open a new project. After the project appears, you need to modify the title of the Windows Form. To do this, change the <b>Text<\/b> property on the right side of the Primal Forms utility, as shown in the following image.<\/p>\n<p class=\"MsoNormal\">\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of changing Text property in PrimalForms\" alt=\"Image of changing Text property in PrimalForms\" width=\"600\" height=\"410\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0301\/hsg-03-01-10-02.jpg\"><\/p>\n<p class=\"Fig-Graphic\">After you have modified the <b>Text <\/b>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.<\/p>\n<p class=\"MsoNormal\">\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of resizing the Windows Form\" alt=\"Image of resizing the Windows Form\" width=\"315\" height=\"170\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0301\/hsg-03-01-10-03.jpg\"><\/p>\n<p class=\"Fig-Graphic\">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 <b>Time<\/b>, and rename the label from <b>label1<\/b> to <b>lbl_DisplayTime<\/b>. The label name is in the <b>Design <\/b>section. This is shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of renaming the label\" alt=\"Image of renaming the label\" width=\"229\" height=\"301\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0301\/hsg-03-01-10-04.jpg\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">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 <b>Font<\/b> tool shown in the following image appears. You can then choose the font, style, and size. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of changing the font\" alt=\"Image of changing the font\" width=\"447\" height=\"357\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0301\/hsg-03-01-10-05.jpg\"><\/p>\n<p class=\"Fig-Graphic\">Drag a <b>Button<\/b> 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 <b>Get Time<\/b> as shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of the Get Time button\" alt=\"Image of the Get Time button\" width=\"600\" height=\"410\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0301\/hsg-03-01-10-06.jpg\"><\/p>\n<p class=\"Fig-Graphic\">In the <b>Design <\/b>section, change the name of the button from <b>button1<\/b> to <b>btn_GetTime<\/b>, as shown in the following image.<\/p>\n<p class=\"MsoNormal\">\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of changing button name\" alt=\"Image of changing button name\" width=\"229\" height=\"301\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0301\/hsg-03-01-10-07.jpg\"><\/p>\n<p class=\"Fig-Graphic\">Now it is time to click <b>Export PowerShell<\/b> 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. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of saving the Windows PowerShell code\" alt=\"Image of saving the Windows PowerShell code\" width=\"571\" height=\"423\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0301\/hsg-03-01-10-08.jpg\"><\/p>\n<p class=\"Fig-Graphic\">Open the clock project Windows PowerShell code in the Windows PowerShell ISE, and modify the code for the <b>OnClick<\/b> event. There are two comments in the code that will help you find this section. After you have located the code for the <b>OnClick<\/b> event, add code that will retrieve the current time, turn it into a string, and assign it to the <b>Text<\/b> property of the <b>$lbl_DisplayTime<\/b> object. This is seen here:<\/p>\n<p class=\"MsoNormal\">\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">#Provide Custom Code for events specified in PrimalForms.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$btn_GetTime_OnClick= <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">{<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">#TODO: Place custom script here<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$lbl_DisplayTime.Text = (get-date).tostring()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When the Scripting Guys Clock runs, the form will appear. Click <b>Get Time<\/b>, and the value of the label changes from <b>Time<\/b> to the current date and time. Anytime you click <b>Get Time<\/b>, the time will be refreshed. This is shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of refreshing time with Get Time button\" alt=\"Image of refreshing time with Get Time button\" width=\"271\" height=\"196\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0301\/hsg-03-01-10-09.jpg\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">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 <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/5180ee3f-2a81-47ec-b89a-1bcac3282894\"><span><font face=\"Segoe\">Script Center Script Repository<\/font><\/span><\/a>. Graphical Windows PowerShell Week will continue tomorrow when we will talk about&hellip;wait a minute. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span><font face=\"Segoe\">Facebook<\/font><\/span><\/a>. If you have any questions, send e-mail to us at <a target=\"_blank\" href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[71,3,4,45],"class_list":["post-51143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-graphical","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51143"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51143\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=51143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}