Dynamic Combobox help please...

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
waddo2018
Posts: 2
Joined: Tue Oct 09, 2018 3:56 am

Dynamic Combobox help please...

Post by waddo2018 » Tue Oct 09, 2018 4:32 am

Hi there,

I'm new to the world of building PowerShell GUI's with Sapien and am looking for some help to do with comboboxes please if anyone has some time...

I've built a basic GUI which uses VMware PowerCLI to pull a list of our "Datastores" into a combobox. The aim is for the user to select their desired "Datastore" from the populated list, and the "FreeSpaceGB" will be displayed below.

I've managed to get it working (to an extent) - but, the issue I'm having is that when I change the "Datastore" in the combobox, the "FreeSpaceGB" doesn't change. You'll see from the attached pictures, datastore BUILD-A-01 has 172.79GB Freespace, but changing to BUILD-A-02 does nothing to the FreeSpaceGB value.
Screenshot1.JPG
Screenshot1.JPG (18.74 KiB) Viewed 208 times
Screenshot2.JPG
Screenshot2.JPG (18.09 KiB) Viewed 208 times
Code:

Code: Select all


Connect-VIServer Build

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '200,200'
$Form.text                       = "Datastore"
$Form.TopMost                    = $false
$Form.AutoSize                   = $true
$Form.BackColor                  = "#717074"
$Form.ForeColor                  = "#ffffff"

$DatastoreLabel                  = New-Object system.Windows.Forms.Label
$DatastoreLabel.text             = "Datastore..."
$DatastoreLabel.AutoSize         = $true
$DatastoreLabel.width            = 150
$DatastoreLabel.height           = 30
$DatastoreLabel.location         = New-Object System.Drawing.Point(20,100)
$DatastoreLabel.ForeColor        = "#ffffff"
$DatastoreLabel.Font             = '10'

$DatastoreCombo                  = New-Object system.Windows.Forms.ComboBox
$DatastoreCombo.text             = ""
$DatastoreCombo.width            = 150
$DatastoreCombo.height           = 30
$DatastoreCombo.BackColor        = "#ffffff"
$DatastoreCombo.ForeColor        = "#717074"
$DatastoreCombo.DropDownStyle    = [System.Windows.Forms.ComboBoxStyle]::DropDownList
@(Get-Datastore | Sort) | ForEach-Object {[void] $DatastoreCombo.Items.Add($_)}
$DatastoreCombo.location         = New-Object System.Drawing.Point(20,150)
$DatastoreCombo.SelectedIndex    = 0

$OutputLabel                     = New-Object System.Windows.Forms.Label
$OutputLabel.text                = "$(Get-Datastore $Datastore.SelectedItem.ToString() | Format-List -Property FreeSpaceGB | Out-String)"
$OutputLabel.width               = 300
$OutputLabel.height              = 300
$OutputLabel.location            = New-Object System.Drawing.Point(20,200)

$Form.controls.AddRange(@($DatastoreLabel,$DatastoreCombo,$OutputLabel))

[void]$Form.ShowDialog()

Having browsed for hours at numerous posts, I can't figure out what I need to do to get the FreeSpaceGB to update. I know I need to add in "SelectedIndexChanged" somewhere, but I've got no idea where, or how. I've had to set this up so that the "$DatastoreCombo.SelectedIndex" is 0, otherwise the "$OutputLabel.text" value doesn't get populated (returned as null). I'm pretty sure it's something simple, any help would be appreciated...

Thanks, Waddo2018

User avatar
jvierra
Posts: 13266
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Dynamic Combobox help please...

Post by jvierra » Tue Oct 09, 2018 8:52 am

I recommend spending some time reading the product manual to learn how to add events to your forms.

On a ComboBox the default event is "SelectedIndexChanged". Just double click in the designer and the correct code will be inserted.

Also read the articles here:

https://info.sapien.com/index.php/guis/gui-design-best-practice/user-interface-design-for-administrators

waddo2018
Posts: 2
Joined: Tue Oct 09, 2018 3:56 am

Re: Dynamic Combobox help please...

Post by waddo2018 » Wed Oct 10, 2018 5:50 am

Thanks jvierra

Locked