none
ComboBox Default value RRS feed

  • Question

  • Helou Guys, 

    pls, how can set a default value from combox?

    Here is a part of the code

    $TaskSequence_Selector_Array = @("Task Sequence 1","Task Sequence 2")
    $TaskSequence_Selector = New-Object System.Windows.Forms.ComboBox
    $TaskSequence_Selector.Location = New-Object System.Drawing.Size(10,35)
    $TaskSequence_Selector.Size = New-Object System.Drawing.Size(180,20)
    $TaskSequence_Selector.DropDownHeight = 200
    $TaskSequence_Selector.Text = "Choose Task Sequence:"
    $TaskSequence_Selector.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
    $TaskSequence_Selector.SetSelected(1,$true)
    $TaskSequence_Selector.Font = New-Object System.Drawing.Font("Arial",10,[System.Drawing.FontStyle]::Regular)
        foreach($TaskSequence_DropDown in $TaskSequence_Selector_Array){
            $TaskSequence_Selector.Items.Add($TaskSequence_DropDown)
        }
    $Form_MainWindow.Controls.Add($TaskSequence_Selector) 

    Now the ComboBox is blank

    How to make some value as default, for example 'Task Sequence 1'?

    Thanks for help.

    Monday, December 30, 2019 12:01 PM

Answers

  • Her is another easier way to build forms by using the ability of types in PowerShell to cast a hash of properties. This save typing and allows us to do neat things like loading properties from a text file directly into a set of controls:

    add-type -AssemblyName system.windows.forms
    $mainWindow = [System.Windows.Forms.Form]@{
        StartPosition = 'CenterScreen'
    }
    $taskSequenceSelector = [System.Windows.Forms.ComboBox]@{
        Location = '10,35'
        Size = '180,20'
        DropDownHeight = 200
        DropDownStyle = 'DropDownList'
        Font = [System.Drawing.Font]'Arial, 10.0pt'
    }
    $mainWindow.Controls.Add($taskSequenceSelector)
    $mainWindow.add_Load({
        $taskSequences = 'Task Sequence 1','Task Sequence 2'
        $taskSequenceSelector.Items.AddRange($taskSequences)
        $taskSequenceSelector.SelectedIndex = $taskSequenceSelector.FindString('Task Sequence 2')
    })
        
    $mainWindow.ShowDialog()


    \_(ツ)_/

    • Marked as answer by LuLaCeK Friday, January 3, 2020 2:17 PM
    Thursday, January 2, 2020 5:10 PM

All replies

  • Just select the row you want in the form load event,

    $TaskSequence_Selector.SelectedIndex = 3

    Also this is how to load an array.

    $TaskSequence_Selector.Items.AddRange($TaskSequence_Selector_Array)


    \_(ツ)_/

    Monday, December 30, 2019 12:09 PM
  • $TaskSequence_Selector.Items.AddRange($TaskSequence_Selector_Array)

    working like a charm. At least, code looks better, thanks ^^ 

    But $TaskSequence_Selector.SelectedIndex = 1

    I got en error: 

    Exception setting "SelectedIndex": "InvalidArgument=Value of '1' is not valid for 'SelectedIndex'.
    Parameter name: SelectedIndex"

    Monday, December 30, 2019 12:16 PM
  • Here are some pointers on how to correctly build forms with PowerShell

    $formMainWindow = New-Object System.Windows.Forms.Form
    $taskSequenceArray = 'Task Sequence 1','Task Sequence 2'
    $taskSequenceSelector = New-Object System.Windows.Forms.ComboBox
    $taskSequenceSelector.Location = '10,35'
    $taskSequenceSelector.Size = '180,20'
    $taskSequenceSelector.DropDownHeight = 200
    $taskSequenceSelector.Text = 'Choose Task Sequence:'
    $taskSequenceSelector.DropDownStyle = 'DropDownList'
    $taskSequenceSelector.Font = [System.Drawing.Font]'Arial, 10.0pt'
    $formMainWindow.add_Load({
        $taskSequenceSelector.Items.AddRange($taskSequenceArray)
        $taskSequenceSelector.SelectedIndex = $taskSequenceSelector.FindString('Task Sequence 2')
    })
        
    $formMainWindow.Controls.Add($taskSequenceSelector)
    $formMainWindow.ShowDialog()


    \_(ツ)_/



    • Edited by jrv Monday, December 30, 2019 12:32 PM
    • Proposed as answer by jrv Thursday, January 2, 2020 3:21 PM
    Monday, December 30, 2019 12:31 PM
  • Her is another easier way to build forms by using the ability of types in PowerShell to cast a hash of properties. This save typing and allows us to do neat things like loading properties from a text file directly into a set of controls:

    add-type -AssemblyName system.windows.forms
    $mainWindow = [System.Windows.Forms.Form]@{
        StartPosition = 'CenterScreen'
    }
    $taskSequenceSelector = [System.Windows.Forms.ComboBox]@{
        Location = '10,35'
        Size = '180,20'
        DropDownHeight = 200
        DropDownStyle = 'DropDownList'
        Font = [System.Drawing.Font]'Arial, 10.0pt'
    }
    $mainWindow.Controls.Add($taskSequenceSelector)
    $mainWindow.add_Load({
        $taskSequences = 'Task Sequence 1','Task Sequence 2'
        $taskSequenceSelector.Items.AddRange($taskSequences)
        $taskSequenceSelector.SelectedIndex = $taskSequenceSelector.FindString('Task Sequence 2')
    })
        
    $mainWindow.ShowDialog()


    \_(ツ)_/

    • Marked as answer by LuLaCeK Friday, January 3, 2020 2:17 PM
    Thursday, January 2, 2020 5:10 PM
  • Hi jrv,

    I rly appreciate your post/help.

    Your code is nice inspiration/example, and does exactly what I want ^^

    Thanks!

    Friday, January 3, 2020 2:17 PM