{"id":6569,"date":"2013-10-01T09:14:00","date_gmt":"2013-10-01T17:14:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=6569"},"modified":"2013-10-01T09:14:33","modified_gmt":"2013-10-01T17:14:33","slug":"powershell-studio-passing-and-returning-values-using-forms","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2013\/10\/01\/powershell-studio-passing-and-returning-values-using-forms\/","title":{"rendered":"PowerShell Studio &ndash; Passing and Returning Values using Forms"},"content":{"rendered":"<p>Two frequent questions we get on the forums are:<\/p>\n<blockquote>\n<p><em>How do I pass a value to another form?<\/em><\/p>\n<\/blockquote>\n<blockquote>\n<p><em>How do I get the results from a form?<\/em> <\/p>\n<\/blockquote>\n<p>In this article, we will answer these common questions in order to give you a better understanding about how to use projects with multiple forms.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Getting Started<\/strong><\/p>\n<p>You will need to create a project with multiple forms in order to proceed. The Multi-Form Project template is a good starting point. At the end of this article there is a link to download a sample project that demonstrates how to use these techniques.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Calling Forms in a Project (Reference Functions)<\/strong><\/p>\n<p>Once you have a project with a form, you can call the form from the Startup.pfs or from another form. To do this, you must use the Form\u2019s Reference Function. A file\u2019s Reference Function uses the following naming convention:<\/p>\n<p><pre><span style=\"color: #008080\">Call-&lt;File Name&gt;_&lt;Extension&gt;<\/span><\/pre>\n<\/p>\n<p>For example, to call the AddExtension.pff file in the sample project, you would use the following reference function:<\/p>\n<pre><span style=\"color: #008080\">Call-AddEmployeeForm_pff<\/span><\/pre>\n<pre><span style=\"color: #008080\"><\/span>&nbsp;<\/pre>\n<p><strong>Calling PS1 Files<\/strong><\/p>\n<p>If you need to call a ps1 file you will have to set the following project file\u2019s properties in the Project Panel as follows:<\/p>\n<table cellspacing=\"0\" cellpadding=\"2\" width=\"400\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"199\"><strong>Property<\/strong><\/td>\n<td valign=\"top\" width=\"199\"><strong>Value<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">Build<\/td>\n<td valign=\"top\" width=\"199\"><em>Include<\/em><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">CommandExtension<\/td>\n<td valign=\"top\" width=\"199\"><em>False<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>By setting the CommandExtension property to <em>False,<\/em> you are telling the project to treat the ps1 file as a function. This allows you use the file\u2019s reference function to execute it:<\/p>\n<pre><span style=\"color: #008080\">Call-MyScript_ps1<br><\/span><\/pre>\n<p>Note: You can still dot source ps1 files; but if you do you must set the ps1 file\u2019s Build setting to <em>Content<\/em> so that it isn\u2019t merged into the final project script.<\/p>\n<p>If you are unsure of the Reference Function name, you can look at the Reference Name property of the project file:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/09\/ReferenceNameinPropertyPanel.png\"><img loading=\"lazy\" decoding=\"async\" title=\"Reference Name in Property Panel\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"Reference Name in Property Panel\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/09\/ReferenceNameinPropertyPanel_thumb.png\" width=\"168\" height=\"244\"><\/a>&nbsp; <\/p>\n<p>In addition, PowerShell Studio\u2019s PrimalSense will display a list of Reference Functions when you type \u201cCall-\u201d :<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" title=\"Reference Function PrimalSense\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"Reference Function PrimalSense\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/09\/ReferenceFunctionPrimalSense.png\" width=\"646\" height=\"106\"> <\/p>\n<p>&nbsp;<\/p>\n<p><strong>Passing Parameters<\/strong><\/p>\n<p>There are times when you need to pass information to a child form. In these situations, you can use a parameter block in the same manor you would with a traditional ps1 script. To do this, go to the form file\u2019s&nbsp; script section and insert a Parameter block at the very top of the script. It is very important that nothing but comments appear before the parameter block.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"Parameter Block\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"Parameter Block\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/09\/ParameterBlock.png\" width=\"567\" height=\"175\"> <\/p>\n<p>&nbsp;<\/p>\n<p>Once you add the parameter block, PowerShell Studio will color the parameters accordingly:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"ReferenceFunction Parameters\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"ReferenceFunction Parameters\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/09\/ReferenceFunctionParameters.png\" width=\"606\" height=\"103\"> <\/p>\n<p>Note: Script scope variables can also be used as an alternative to parameters.<\/p>\n<p><strong>Returning Values<\/strong><\/p>\n<p>Now that you know how to pass values to a form, how do you get values back? Returning values depends on what you are referencing. For example, Form files are handled differently the ps1 files.<\/p>\n<p><em>Form Return Values<\/em><\/p>\n<p>The Form\u2019s Reference Function returns the DialogResult of the Form when it is closed. How do you handle return values?<\/p>\n<p>Luckily PowerShell Studio auto generates variable values with the values of controls in the form. The return variables name convention is as follows:<\/p>\n<p><pre><span style=\"color: #8b0000\">$&lt;Form Name&gt;_&lt;Control Name&gt;<\/span><\/pre>\n<\/p>\n<p>For example, if AddEmployeeForm.pff has a TextBox named textBoxFirstName, PowerShell Studio will generate the following return variable:<\/p>\n<pre><span style=\"color: #8b0000\">$AddEmployeeForm_textboxFirstName<\/span><\/pre>\n<p>When you use a Reference Function, PowerShell Studio\u2019s PrimalSense will automatically include the return variables when you type $.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"ReturnVariables\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"ReturnVariables\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/09\/ReturnVariables.png\" width=\"640\" height=\"243\"> <\/p>\n<p>&nbsp;<\/p>\n<p>The value contained in the variable depends on the control. The following is a list of controls that generate return values:<\/p>\n<table cellspacing=\"0\" cellpadding=\"2\" width=\"400\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"199\"><strong>Control<\/strong><\/td>\n<td valign=\"top\" width=\"199\"><strong>Returns<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">TextBox<\/td>\n<td valign=\"top\" width=\"199\">string<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">CheckBox<\/td>\n<td valign=\"top\" width=\"199\">bool<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">ComboBox<\/td>\n<td valign=\"top\" width=\"199\">string<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">MonthCalendar<\/td>\n<td valign=\"top\" width=\"199\">DateTime<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">DateTimePicker<\/td>\n<td valign=\"top\" width=\"199\">DateTime<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">ListView<\/td>\n<td valign=\"top\" width=\"199\">Collection<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">ListBox<\/td>\n<td valign=\"top\" width=\"199\">Collection<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">NumericUpDown<\/td>\n<td valign=\"top\" width=\"199\">Decimal<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">RichTextBox<\/td>\n<td valign=\"top\" width=\"199\">string<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">TreeView<\/td>\n<td valign=\"top\" width=\"199\">string<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">RadioButton<\/td>\n<td valign=\"top\" width=\"199\">bool<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">CheckedListBox<\/td>\n<td valign=\"top\" width=\"199\">Collection<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><em><em>Returning Custom Values in Forms<\/em><\/em><\/p>\n<p>In the case where the control doesn\u2019t return a value or you want to return a custom value \/ object, you can create a variable using the script scope:<\/p>\n<pre><span style=\"color: #8b0000\">$script:MyReturnValue<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">..<\/span><span style=\"color: #000000\">.<\/span><\/pre>\n<p>Then you can reference the variable in the file that called the form.<\/p>\n<pre><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008080\">Call-AddEmployeeForm_pff<\/span><span style=\"color: #000000\">) \u2013eq <\/span><span style=\"color: #ff0000\">'OK'<\/span><span style=\"color: #000000\">)\n{\n    <\/span><span style=\"font-weight: bold; color: #0000ff\">Out-Default<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$script:MyReturnValue<\/span><span style=\"color: #000000\">\n}<\/span><\/pre>\n<p><em><\/em>In the sample project, the amount of time the user took to complete the Add Employee form is saved in a script variable. The main form then displays a message if the user took longer than the allotted time.<\/p>\n<p>&nbsp;<br \/><em>Script Return Values<\/em><\/p>\n<p>Script (ps1) files simply return the objects via the pipeline as there is no form to return a <em>DialogResult<\/em>.<\/p>\n<pre><span style=\"color: #008080\">Call-MyScript_ps1<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">|<\/span><span style=\"color: #000000\"> <\/span><span style=\"font-weight: bold; color: #0000ff\">Out-Default<\/span><\/pre>\n<p>Hopefully now you have the tools to create complex GUIs that utilize multiple forms. The sample project below utilizes the techniques covered by this article. <\/p>\n<p><strong>Sample Project \u2013 Employee Roster<\/strong><\/p>\n<p>The Employee Roster is a sample project that uses a grid to display a list of employees. When you press a button, a second form is displayed to collect employee information and the results are in turn loaded into the grid of the first form.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/09\/EmployeeRosterProject.png\"><img loading=\"lazy\" decoding=\"async\" title=\"Employee Roster Project\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"Employee Roster Project\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/09\/EmployeeRosterProject_thumb.png\" width=\"244\" height=\"123\"><\/a> <\/p>\n<p><a href=\"http:\/\/www.sapien.com\/downloads#Sample Scripts\/PassingAndReturningValuesSampleProject.zip\"><img loading=\"lazy\" decoding=\"async\" title=\"DOWNLOAD2\" style=\"border-top: 0px; border-right: 0px; border-bottom: 0px; margin-left: 0px; border-left: 0px; display: inline; margin-right: 0px\" border=\"0\" alt=\"DOWNLOAD2\" align=\"left\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/10\/DOWNLOAD2.png\" width=\"109\" height=\"27\"><\/a> <\/p>\n<\/p>\n<p>&#8211; Employee Roster Sample Project<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Two frequent questions we get on the forums are: How do I pass a value to another form? How do I get the results from a form? In this article, we will answer these common questions in order to give you a better understanding about how to use projects with multiple forms. &nbsp; Getting Started [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[705,25],"tags":[41,850,28,1016,767,312],"class_list":["post-6569","post","type-post","status-publish","format-standard","hentry","category-powershell-studio-2012","category-windows-powershell","tag-gui","tag-parameters","tag-powershell","tag-powershell-studio","tag-projects","tag-winforms"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/6569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=6569"}],"version-history":[{"count":6,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/6569\/revisions"}],"predecessor-version":[{"id":6578,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/6569\/revisions\/6578"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=6569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=6569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=6569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}