SAPIEN PowerShell Studio – Simple sample using ComboBox in forms

sapienstudio_00_2016-11-11_14-44-34

The Form

This is a simple form showing the ComboBox component using PowerShell Studio.

sapien_cb_01_2016-11-24_9-16-05

sapien_cb_02_2016-11-24_9-16-05

For this sample I’m using a Simple Form in order to add components to it. In this case a ComboBox Component. At the same time we are adding a Label Component which I will use to display the selected item from the ComboBox.

sapien_cb_03_2016-11-24_9-16-05

The Code

Notice in the “Script” pane, when you create the form and added the ComboBox, it will also add a new function: “Load-ComboBox“.

sapien_cb_04_2016-11-24_9-16-05

This function,m under commented help, will give you examples on how to load items into the ComboBox component for the form to use.

sapien_cb_05_2016-11-24_9-16-05

This will make your life easy while building your forms.

But, you can provide your own script code to populate the component. Here’s an example code snippet:

## - Custom Code to Load into ComboBox:
[array]$lstItems = ("Server1", "Server2");
$cbPickLstItems.Items.Clear();
$cbPickLstItems.Items.AddRange($lstItems);
$cbPickLstItems.Text = $lstItems[0].ToString();

Of course, you can put the code into a function to keep consistency across the application.

Next, would be to create the code to add items into the ComboBox and compile the application. This code will be added into the “$ComboBox_Load” event. I’m using the SAPIEN already provided “Load-ComboBox” function.

sapien_cb_06_2016-11-24_9-16-05

Now, just run the application, save, and see the results.

sapien_cb_07_2016-11-24_9-16-05

sapien_cb_08_2016-11-24_9-16-05

sapien_cb_09_2016-11-24_9-16-05

The code we provided will only select the item. You’ll notice the results is not displayed yet.

In order to display the selected item we need to¬†add another event called “.._SelectedIndexChanged“.

In order to create this event, go to the form and double-click on the ComboBox component and then we add the following code in the “$combobox1_SelectedIndexChanged” event.

$results = $ComboBox1.SelectedItem.ToString();
$labelResultsHere.Text = $results;

Then, run the application event and we finally see the displayed selected results.

sapien_cb_10_2016-11-24_9-16-05

sapien_cb_11_2016-11-24_9-16-05

sapien_cb_12_2016-11-24_9-16-05

It’s always fun to build quick apps with PowerShell Studio!

This entry was posted in PowerShell. Bookmark the permalink.