none
Powershell ComboBox

    Question

  • Hi,

    Can someone explaine me how can I get out of value from Combobox function to label or textbox. I have the code like this:

    Function computers{

    $computerNames = Get-Content -Path c:\computers.txt
     $comboBox1.BeginUpdate()
          foreach($computer in $computerNames)
            {
             $comboBox1.Items.add($computer)
            }
     $comboBox1.EndUpdate()

    }

    $label1.text = $comboBox1.SelectedItem.ToString()

    Thank you,

    Kaido

    Thursday, October 07, 2010 1:57 PM

Answers

  • Here is an example:

     

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    
    $Form1 = New-Object System.Windows.Forms.Form
    $Form1.ClientSize = New-Object System.Drawing.Size(407, 390)
    $form1.topmost = $true
    
    
    $computerNames = @(1,2,3)
    $comboBox1 = New-Object System.Windows.Forms.ComboBox
    $comboBox1.Location = New-Object System.Drawing.Point(25, 55)
    $comboBox1.Size = New-Object System.Drawing.Size(350, 310)
    foreach($computer in $computerNames)
    {
      $comboBox1.Items.add($computer)
    }
    $Form1.Controls.Add($comboBox1)
    
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Point(25, 20)
    $Button.Size = New-Object System.Drawing.Size(98, 23)
    $Button.Text = "Output"
    $Button.add_Click({$label.Text = $comboBox1.SelectedItem.ToString()})
    $Form1.Controls.Add($Button)
    
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(70, 90)
    $label.Size = New-Object System.Drawing.Size(98, 23)
    $label.Text = ""
    $Form1.Controls.Add($label)
    
    [void]$form1.showdialog()
    

    • Marked as answer by Kaido1000MVP Tuesday, October 12, 2010 7:48 AM
    Sunday, October 10, 2010 9:15 AM