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

All replies

  • 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
  • do you have an example of doing the above with xAML?

    golu

    Wednesday, October 08, 2014 2:17 PM
  • Hi Golu.

    You should open a new question for this.

    but to point you in the right direction, I used this link: http://learn-powershell.net/2012/12/02/powershell-and-wpf-listbox/

    and came up with this code:

    #Build the GUI
    [xml]$xaml = @"
    <Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen" ResizeMode="NoResize"
        Width = "313" Height = "425" ShowInTaskbar = "True" Background = "lightgray"> 
        <StackPanel >
            <TextBox x:Name="readonlyTextBox" IsReadOnly="True" TextWrapping="Wrap">
                Type something into the text box below and click Add to update the listbox.
            </TextBox>
            <TextBox x:Name="inputTextBox" />
            <Button x:Name="addButton" Content="Add"/>
            <Button x:Name="removeButton" Content="Remove Selected Item/s"/>
            <ListBox x:Name="listbox" MinHeight = "50" AllowDrop="True" SelectionMode="Extended"/>
            <ComboBox x:Name="combobox" />
        </StackPanel>
    </Window>
    "@
     
    $reader=(New-Object System.Xml.XmlNodeReader $xaml)
    $Window=[Windows.Markup.XamlReader]::Load( $reader )
     
    #Connect to Controls
    $inputTextBox = $Window.FindName('inputTextBox')
    $addButton = $Window.FindName('addButton')
    $listbox = $Window.FindName('listbox')
    $removeButton = $Window.FindName('removeButton')
    $combobox = $Window.FindName('combobox')
     
    #Events
    $addButton.Add_Click({
        If ((-NOT [string]::IsNullOrEmpty($inputTextBox.text))) {
            $listbox.Items.Add($inputTextBox.text)
            $combobox.Items.Add($inputTextBox.text)
            $inputTextBox.Clear()
        }
    })
    $removeButton.Add_Click({
        While ($listbox.SelectedItems.count -gt 0) {
            $listbox.Items.RemoveAt($listbox.SelectedIndex)
        }
    })
    $listbox.Add_Drop({
        (Get-Content $_.Data.GetFileDropList()) | ForEach {
            $listbox.Items.Add($_)
        }
    })
    $Window.ShowDialog() | Out-Null


    If you found this post helpful, please "Vote as Helpful". If it answered your question, remember to "Mark as Answer"
    MCC & PowerShell enthusiast
    http://oliver.lipkau.net/blog

    • Proposed as answer by Oliver Lipkau Thursday, October 16, 2014 8:42 AM
    Thursday, October 09, 2014 9:43 AM