Skip to content

ComboBox

In a ScriptoForm, the Microsoft .NET ComboBox class is a control that allows a user to interactively select one of multiple items from a drop-down style list. This selected text or index can be used to control or provide a parameter to actions performed by a ScriptoForm.

ScriptoForm with example ComboBox control

In a ScriptoForm PowerShell script file, a ComboBox control should be instantiated in the Controls region and then defined within the Forms script block. A ComboBox is usually paired with a matching Label control. If using the ScriptoForm VS Code snippets, the ScriptoForm Controls: Label & ComboBox snippet can be used to instantiate this control pair and the ScriptoForm Properties: Label & ComboBox snippet can be used to assign a default set of properties to them. By convention, this control pair should be nested in the $GroupBoxMain GroupBox container.

The steps needed to implement a ComboBox control with a ScriptoForm include:

  • Instantiate a ComboBox object and an associated Label object in the Controls region.
    Suggested name format: $LabelName | $ComboBoxName
  • Assign the required and any optional properties to the ComboBox object in the Forms region.
  • Assign Event Handlers to the ComboBox object in the Forms region.
  • Add the control pair to a GroupBox object in the Forms region.
  • Define event handlers for the ComboBox with script blocks in the Handlers region.
  • Add an initial list of items to the ComboBox in an appropriate event handler in the Handlers region, as needed.
  • Modify the list of items in the ComboBox with appropriate event handlers in the Handlers region, as needed.
  • Utilize the value of the ComboBox Text field with appropriate event handlers in the Handlers region, as needed.
  1. Instantiate a new System.Windows.Forms.ComboBox and System.Windows.Forms.Label control pair:
    Controls Region
    #region Controls
    $FormMain = New-Object -TypeName System.Windows.Forms.Form
    $GroupBoxMain = New-Object -TypeName System.Windows.Forms.GroupBox
    <#
    Instantiate control objects here...
    #>
    $LabelEnvironment = New-Object -TypeName System.Windows.Forms.Label
    $ComboBoxEnvironment = New-Object -TypeName System.Windows.Forms.ComboBox
    $ButtonRun = New-Object -TypeName System.Windows.Forms.Button
    $ButtonClose = New-Object -TypeName System.Windows.Forms.Button
    $StatusStripMain = New-Object -TypeName System.Windows.Forms.StatusStrip
    $ToolStripStatusLabelMain = New-Object -TypeName System.Windows.Forms.ToolStripStatusLabel
    #endregion
  2. Assign properties to the pair, the event handler listener to the ComboBox object, add the pair to the GroupBox object, and add the $FormMain_Load handler to the main Form:
    Forms Region | $ShowMainForm
    #region Forms
    $ShowFormMain =
    {
    $FormWidth = 330
    $FormHeight = 260
    $FormMain.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Process -Id $PID).Path)
    $FormMain.Text = "Title"
    $FormMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
    $FormMain.ClientSize = New-Object -TypeName System.Drawing.Size($FormWidth,$FormHeight)
    $FormMain.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
    $FormMain.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
    $FormMain.MaximizeBox = $false
    $FormMain.AcceptButton = $ButtonRun
    $FormMain.CancelButton = $ButtonClose
    $FormMain.Add_Shown($FormMain_Shown)
    $FormMain.Add_Load($FormMain_Load)
    $GroupBoxMain.Location = New-Object -TypeName System.Drawing.Point(10,5)
    $GroupBoxMain.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 20),($FormHeight - 80))
    $FormMain.Controls.Add($GroupBoxMain)
    <#
    Assign control properties and event handlers here and add to the GroupBox...
    #>
    $LabelEnvironment.Location = New-Object -TypeName System.Drawing.Point(15,70)
    $LabelEnvironment.AutoSize = $true
    $LabelEnvironment.Text = "Environment:"
    $GroupBoxMain.Controls.Add($LabelEnvironment)
    $ComboBoxEnvironment.Location = New-Object -TypeName System.Drawing.Point(15,90)
    $ComboBoxEnvironment.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 50),20)
    $ComboBoxEnvironment.TabIndex = 1
    $ComboBoxEnvironment.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
    $ComboBoxEnvironment.Add_SelectedIndexChanged($ComboBoxEnvironment_SelectedIndexChanged)
    $GroupBoxMain.Controls.Add($ComboBoxEnvironment)
    $ButtonRun.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 175),($FormHeight - 60))
    $ButtonRun.Size = New-Object -TypeName System.Drawing.Size(75,25)
    $ButtonRun.TabIndex = 100
    $ButtonRun.Enabled = $false
    $ButtonRun.Text = "Run"
    $ButtonRun.Add_Click($ButtonRun_Click)
    $FormMain.Controls.Add($ButtonRun)
    $ButtonClose.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 85),($FormHeight - 60))
    $ButtonClose.Size = New-Object -TypeName System.Drawing.Size(75,25)
    $ButtonClose.TabIndex = 101
    $ButtonClose.Text = "Close"
    $ButtonClose.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $FormMain.Controls.Add($ButtonClose)
    $StatusStripMain.SizingGrip = $false
    $StatusStripMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
    [void]$StatusStripMain.Items.Add($ToolStripStatusLabelMain)
    $FormMain.Controls.Add($StatusStripMain)
    [void]$FormMain.ShowDialog()
    $FormMain.Dispose()
    }
    #endregion
  3. Add items to the ComboBox object and set the default index for it using the $FormMain_Load event handler:
    Handlers Region | $FormMain_Load
    $FormMain_Load =
    {
    if (Test-Path -Path $EnvironmentsFile)
    {
    $ComboBoxEnvironment.Items.AddRange($(Get-Content -Path $EnvironmentsFile))
    $ComboBoxEnvironment.SelectedIndex = 0
    }
    }
  4. Create the event handler for the ComboBox object:
    Handlers Region | $ComboBoxEnvironment_SelectedIndexChanged
    $ComboBoxEnvironment_SelectedIndexChanged =
    {
    if ($ComboBoxEnvironment.Text = "Production")
    {
    $TextBoxEmailAddress.Enabled = $true
    }
    }
  5. Use the Text property of ComboBox object to retrive the current value:
    Handlers Region | $ButtonRun_Click
    $ButtonRun_Click =
    {
    Invoke-FormAction -Action {
    try
    {
    Start-Sleep -Seconds 2
    [void][System.Windows.Forms.MessageBox]::Show(
    "Simulation of work completed.`n`nServer: $($TextBoxServerName.Text)`nEnvironment: $($ComboBoxEnvironment.Text)",
    "Results",
    [System.Windows.Forms.MessageBoxButtons]::OK,
    [System.Windows.Forms.MessageBoxIcon]::Information
    )
    }
    catch
    {
    [void][System.Windows.Forms.MessageBox]::Show(
    $PSItem.Exception.Message + "`n`nPlease contact $SUPPORT_CONTACT for technical support.",
    "Exception",
    [System.Windows.Forms.MessageBoxButtons]::OK,
    [System.Windows.Forms.MessageBoxIcon]::Warning
    )
    }
    } -Reset {
    $ComboBoxEnvironment.SelectedIndex = 0
    }
    }
The DropDownStyle property of a ComboBox determines one of three ComboBoxStyle behaviors the ComboBox will exhibit. Typically, in a ScriptoForm, the DropDownList style is used, which indicates the list is non-interactive and cannot be modified by the user.
Forms Region | $ShowMainForm
$ComboBoxEnvironment.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList

ComboBox Class (System.Windows.Forms) | Microsoft Learn