Link to home
Start Free TrialLog in
Avatar of DevSupport
DevSupport

asked on

powershell code to list items in dropdown

Hi

I am using the below to generate a list of databases from a database server based on selection from list box:
$objLabel0 = New-Object System.Windows.Forms.Label
$objLabel0.Location = New-Object System.Drawing.Size(20,22) 
$objLabel0.Size = New-Object System.Drawing.Size(280,20) 
$objLabel0.Text = "Select the Source Database Group Name:"
    $objForm.Controls.Add($objLabel0)

$objListBox0 = New-Object System.Windows.Forms.ListBox 
$objListBox0.Location = New-Object System.Drawing.Size(20,42) 
$objListBox0.Size = New-Object System.Drawing.Size(260,20) 
$objListBox0.Height = 80

[void] $objListBox0.Items.Add("server1")
[void] $objListBox0.Items.Add("Server2")
.
.
.
    $objForm.Controls.Add($objListBox0) 

$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(20,120) 
$objLabel1.Size = New-Object System.Drawing.Size(280,20) 
$objLabel1.Text = "Enter Source Database Name:"
    $objForm.Controls.Add($objLabel1)

    $objButton2 = New-Object System.Windows.Forms.Button
	$objButton2.Location = New-Object System.Drawing.Size(290,140)
	$objButton2.Size = New-Object System.Drawing.Size(75,20)
	$objButton2.Text = "Load DBs"
	$objButton2.Add_Click({PopulateDBList2})
$objForm.Controls.Add($objButton2)

function PopulateDBList2{
$objDropDown1 = New-Object System.Windows.Forms.ComboBox
$objDropDown1.Location = New-Object System.Drawing.Size(20,140)
$objDropDown1.Size = New-Object System.Drawing.Size(260,15)
$DBNamearr1 = (Invoke-SQLCmd -query "SELECT name FROM sys.databases where database_id > '4'" -Server $objListBox0.SelectedItem)
$objDropDown1.Items.Clear();
ForEach ($item in $DBNamearr1){
    $objDropDown1.items.add($item.Name)
}
$objForm.Controls.Add($objDropDown1)
}

Open in new window


Please see the populatedblist2 function, I am able to get the list of databases into the combo box.

However, when I click the button again by selecting a different item in the Listbox, I am not able to retrieve results again.

In other words I can load the names from select query only once. Please let me know how to regenerate this list in my subsequent button presses.

Thanks
Avatar of DevSupport
DevSupport

ASKER

Please let me know if my question is not clear enough.

I am attaching the full code if you need.

Thanks
powershellform_refresh.txt
Please let me know if there is no solution and I will work on some alternative solution.

Thanks
Avatar of Qlemo
I cannot see any reason for that behaviour, so - did you check if the populatedblist2 code is been executed again when you click the button?
To help further, we would need to have runnable code, reduced to the minimum possible.
I can see that the populatedblist2 function is executing again. I did a test write-host command and that works each time I press button but the drop down list does not change.

Thanks
Amrith
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial