Welcome › Forums › General PowerShell Q&A › Use choice from one drop down box as variable in another
- This topic has 1 reply, 2 voices, and was last updated 6 months, 1 week ago by
Participant.
Viewing 1 reply thread
-
AuthorPosts
-
-
July 9, 2020 at 7:08 pm #241397
I need to get the results of the first DROPDOWN box stored as a variable, and used in place of INSTANCE_NAME in the second DROPDOWN box.
Essentially, the SELECT query from the second box needs to be run against the MASTER database in the SLQ_INSTANCE that the first DROPDOWN box results in once a choice has been made.
I’ve tried a few variations and can’t seem to get it right. Any suggestions?
Thanks in advance!
PowerShell1234567891011121314151617181920212223242526272829303132333435#first DROPDOWN Box$label = New-Object System.Windows.Forms.Label$label.Location = New-Object System.Drawing.Point(10,20)$label.Size = New-Object System.Drawing.Size(280,20)$label.Text = 'SQL Server Name'$form.Controls.Add($label)$DropDownBox = New-Object System.Windows.Forms.ComboBox$DropDownBox.Location = New-Object System.Drawing.Size(10,40)$DropDownBox.Size = New-Object System.Drawing.Size(260,20)$DropDownBox.DropDownHeight = 200$Form.Controls.Add($DropDownBox)$wksList= invoke-sqlcmd -query "select * from VIEW_NAMEorder by instance_name" -database DATABASE_NAME -serverinstance INSTANCE_NAMEforeach ($wks in $wksList) {$DropDownBox.Items.Add($wks.Instance_Name)} #end foreach#end first DROPDOWN box#second DROPDOWN Box$label2 = New-Object System.Windows.Forms.Label$label2.Location = New-Object System.Drawing.Point(10,90)$label2.Size = New-Object System.Drawing.Size(280,20)$label2.Text = 'Database Name'$form.Controls.Add($label2)$DropDownBox2 = New-Object System.Windows.Forms.ComboBox$DropDownBox2.Location = New-Object System.Drawing.Size(10,110)$DropDownBox2.Size = New-Object System.Drawing.Size(260,20)$DropDownBox2.DropDownHeight = 200$Form.Controls.Add($DropDownBox2)$wksList2= invoke-sqlcmd -query "select name from sys.databaseswhere database_id>4order by name" -database MASTER -serverinstance INSTANCE_NAMEforeach ($wks in $wksList2) {$DropDownBox2.Items.Add($wks.name)} #end foreach#end second DROPDOWN box -
July 10, 2020 at 10:28 am #241547
Take a look at this example;
PowerShell1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253$DropDownBox.DropDownHeight = 200$Form.Controls.Add($DropDownBox)#second DROPDOWN Box$label2 = New-Object System.Windows.Forms.Label$label2.Location = New-Object System.Drawing.Point(10,90)$label2.Size = New-Object System.Drawing.Size(280,20)$label2.Text = 'Database Name'$form.Controls.Add($label2)$DropDownBox2 = New-Object System.Windows.Forms.ComboBox$DropDownBox2.Location = New-Object System.Drawing.Size(10,110)$DropDownBox2.Size = New-Object System.Drawing.Size(260,20)$DropDownBox2.DropDownHeight = 200$Form.Controls.Add($DropDownBox2)function Load-DrpDwn {$DropDownBox2.Items.Clear()$wksList2= Get-Process -Name $script:mDbforeach ($wks in $wksList2) {[void]$DropDownBox2.Items.Add($wks.name)} #end foreach#end second DROPDOWN box$DropDownBox2.SelectedIndex = 0}#Load dropdown servers$wksList = Get-Processforeach ($wks in $wksList) {[void]$DropDownBox.Items.Add($wks.Name)}#Set first item in list$DropDownBox.SelectedIndex = 0#Set script variable for selected item$script:mDB = $DropDownBox.SelectedItem#A better way to do this is https://info.sapien.com/index.php/guis/gui-controls/spotlight-on-the-combobox-control#But quick and dirty, we want to create a function to dynamically clear and load the second dd because it will get#called initially on load and again when something changes in dd1Load-DrpDwn#Event for when something is selected to update dd2$DropDownBox_SelectedIndexChanged = {$script:mDB = $DropDownBox.SelectedItemLoad-DrpDwn}$DropDownBox.add_SelectedIndexChanged($DropDownBox_SelectedIndexChanged)$form.ShowDialog()
-
-
AuthorPosts
Viewing 1 reply thread
- The topic ‘Use choice from one drop down box as variable in another’ is closed to new replies.