locked
dropdownbox add.items RRS feed

  • Question

  • Hi guys,

    i have a function that gets all instances on a sql server and this is piped via format-table

    Next i want to create a windows form with a dropdownlist, listing the result from format-table on InstanceName

    But for some reason either the list is empty or only showing 'system-object[]' , but not the table results for 'InstanceName'

    any suggestions? THX

    the powerqhell script:

    $InstanceTable = Get-SqlInstanceInfo SQL2016 | FT -Property InstanceName -> table shows 2 results in my test = ok

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $Form = New-Object System.Windows.Forms.Form    
    $Form.Size = New-Object System.Drawing.Size(400,350)
    $Form.StartPosition = "CenterScreen"
    $Font = New-Object System.Drawing.Font("Arial",8,[System.Drawing.FontStyle]::Bold)
    $Form.Font = $Font 
    $Form.Text = "SQL Instance Selection"
    $Form.MaximizeBox = $false
    $Form.FormBorderStyle = 'Fixed3D'

    #****************************************************************************
    # What we’re doing here is configuring our form so we can do two things:
    # Use the ENTER key instead of the OK button.                            
    # Use the ESC key instead of the Cancel button.                          

    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
        {$Form.Close()}})
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
        {$Form.Close()}})

    #****************************************************************************
    # Add the OK and Cancel buttons:
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(195,280)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$Form.Close()})
    $Form.Controls.Add($OKButton)

    $QuitButton = New-Object System.Windows.Forms.Button
    $QuitButton.Location = New-Object System.Drawing.Size(285,280)
    $QuitButton.Size = New-Object System.Drawing.Size(75,23)
    $QuitButton.Text = "Quit"
    $QuitButton.Add_Click({stop-process -Id $PID})
    $Form.Controls.Add($QuitButton)

    #****************************************************************************
    # Creating entry fields:

    $DropDownBox = New-Object System.Windows.Forms.ComboBox
    $DropDownBox.Location = New-Object System.Drawing.Size(20,30) 
    $DropDownBox.Size = New-Object System.Drawing.Size(180,20) 
    $DropDownBox.DropDownHeight = 200
    $DropDownBox.DropDownStyle = 2


    $Label = New-Object System.Windows.Forms.Label
    $Label.Location = New-Object System.Drawing.Size(20,10) 
    $Label.Size = New-Object System.Drawing.Size(180,20) 
    $Label.Text = "Select SQL Instance:"

    #****************************************************************************
    # Add all items to DropdownBox :

    ForEach ($instancename in $InstanceTable ) {
    [void]$DropDownBox.Items.Add($instancename)} 

    #****************************************************************************
    # Add all created entry fields to the form :

    $Form.Controls.Add($DropDownBox)
    $Form.Controls.Add($Label)


    #****************************************************************************
    # Activate and Show the Form :
      
    $Form.Add_Shown({$Form.Activate()})
    $Form.Topmost = $True
    [void]$Form.ShowDialog()

    Monday, July 31, 2017 11:36 AM

Answers

  • Format-Table returns a PowerShell format Object. Use select-object.

    InstanceTable = Get-SqlInstanceInfo SQL2016 | select-object -Property InstanceName 

    or 

    InstanceTable = (Get-SqlInstanceInfo SQL2016).InstanceName 


    Cheers,

    Ruud
    Twitter:    Blog: AzureStack.Blog  LinkedIn:    
    Note: Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.



    Monday, July 31, 2017 11:58 AM

All replies

  • Try storing the SQL instances in an Array instead of a variable
    Monday, July 31, 2017 11:52 AM
  • Format-Table returns a PowerShell format Object. Use select-object.

    InstanceTable = Get-SqlInstanceInfo SQL2016 | select-object -Property InstanceName 

    or 

    InstanceTable = (Get-SqlInstanceInfo SQL2016).InstanceName 


    Cheers,

    Ruud
    Twitter:    Blog: AzureStack.Blog  LinkedIn:    
    Note: Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.



    Monday, July 31, 2017 11:58 AM