Populating a second dropdown list based on your choice in the first dropdown list
Following on from my post yesterday in the week regarding the creating of forms in Powershell, I came across a situation that I need to work on. I have a form that contains 2 dropdown lists. The first list contains a list of all the volumes that are on my NetApp filer. The second dropdown list is populated with a list of all the snapshots from which ever volume I choose in the first dropdown list.
When I change my choice in dropdown list 1 I want to see the contents of dropdown list 2 change and reflect my choice by listing the snapshots of the selected volume in dropdown list 1.
So here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# import the Data-ONTap module Import-module DataONTAP # Connect to you Netapp filer Connect-NaController "NetApp filer Name" # change this to your filer name # Get a list of all filer volumes [array]$MyDropDownList1 = Get-NaVol # Function to return the name of the selected snaphsot from the second DropDown box function Return-DropDown { $script:Choice = $DropDown2.SelectedItem.ToString() $Form.Close() } # Function tp populate the second DropDown box with a list of snapshots of the choosen volume # in the first dropdown list function populate_snapshots{ # The line below clears the DropDown2 list of snapshots $DropDown2.Items.Clear() $volume = $DropDown1.SelectedItem [array]$MyDropDownList2 = Get-NaSnapshot -TargetName $volume ForEach ($Item in $MyDropDownList2) { [void] $DropDown2.Items.Add($Item) } } function SelectSnapshot{ [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") # Set the size of your form $Form = New-Object System.Windows.Forms.Form $Form.width = 500 $Form.height = 200 $Form.Text = ”Multiple DropDown boxes” # Set the font of the text to be used within the form $Font = New-Object System.Drawing.Font("Times New Roman",12) $Form.Font = $Font $DropDown1 = new-object System.Windows.Forms.ComboBox $DropDown1.Location = new-object System.Drawing.Size(130,20) $DropDown1.Size = new-object System.Drawing.Size(280,40) $DropDown2 = new-object System.Windows.Forms.ComboBox $DropDown2.Location = new-object System.Drawing.Size(130,60) $DropDown2.Size = new-object System.Drawing.Size(280,40) $Button = new-object System.Windows.Forms.Button $Button.Location = new-object System.Drawing.Size(130,100) $Button.Size = new-object System.Drawing.Size(100,40) $Button.Text = "Select Snapshot" $Button.Add_Click({Return-DropDown}) $form.Controls.Add($Button) $CancelButton = new-object System.Windows.Forms.Button $CancelButton.Location = new-object System.Drawing.Size(255,100) $CancelButton.Size = new-object System.Drawing.Size(100,40) $CancelButton.Text = "Cancel" $CancelButton.Add_Click({$Form.Close()}) $form.Controls.Add($CancelButton) ForEach ($Item in $MyDropDownList1) { [void] $DropDown1.Items.Add($Item.Name) } $Form.Controls.Add($DropDown1) $DropDown1.Add_SelectedIndexChanged({populate_snapshots}) $Form.Controls.Add($DropDown2) $Form.Add_Shown({$Form.Activate()}) [void] $Form.ShowDialog() return $script:choice } # call the function $option = @() $option = SelectSnapshot # Return your selection in text format Write-Host $option |
Feel free to leave a comment. I am hoping to have more Powershell/Form related posts soon.
I’m having a problem with the next drop down. It doesn’t add the value into anything, Am I doing something wrong?
$DropDownLabelTitle = New-Object System.Windows.Forms.Label
$DropDownLabelTitle.Location = New-Object System.Drawing.Size(10,170)
$DropDownLabelTitle.Size = New-Object System.Drawing.Size(280,20)
$DropDownLabelTitle.Text = “Please Select the Title from the Drop Down List: ”
$objForm.Controls.Add($DropDownLabelTitle)
[array]$DropDownArrayTitle = “Title1”, “Title2”, “Title3”
$DropDownTitle = New-Object System.Windows.Forms.ComboBox
$DropDownTitle.Location = New-Object System.Drawing.Size(10,190)
$DropDownTitle.Size = New-Object System.Drawing.Size(260,20)
ForEach ($TitleDrop in $DropDownArrayTitle){
$DropDownTitle.Items.Add($TitleDrop)
}
$objForm.Controls.Add($DropDownTitle)
$DropDownTitle.Add_SelectedIndexChanged({
If ($DropDownTitle.Text -eq “Title1”){$DropDownLabelRank = New-Object System.Windows.Forms.Label
$DropDownLabelRank.Location = New-Object System.Drawing.Size(10,220)
$DropDownLabelRank.Size = New-Object System.Drawing.Size(280,20)
$DropDownLabelRank.Text = “Please Select the Name from the Drop Down List: ”
$objForm.Controls.Add($DropDownLabelRank)
[array]$DropDownArrayRank = “Something1”, “Something2”, “Something3”
$DropDownRank = New-Object System.Windows.Forms.ComboBox
$DropDownRank.Location = New-Object System.Drawing.Size(10,240)
$DropDownRank.Size = New-Object System.Drawing.Size(260,20)
ForEach ($RankDrop in $DropDownArrayRank){
$DropDownRank.Items.Add($RankDrop)
}$objForm.Controls.Add($DropDownRank)}
ElseIf ($DropDownTitle.Text -eq “Title2”){$DropDownLabelRank = New-Object System.Windows.Forms.Label
$DropDownLabelRank.Location = New-Object System.Drawing.Size(10,220)
$DropDownLabelRank.Size = New-Object System.Drawing.Size(280,20)
$DropDownLabelRank.Text = “Please Select the Something from the Drop Down List: ”
$objForm.Controls.Add($DropDownLabelRank)
[array]$DropDownArrayRank = “SomethingElse”
$DropDownRank = New-Object System.Windows.Forms.ComboBox
$DropDownRank.Location = New-Object System.Drawing.Size(10,240)
$DropDownRank.Size = New-Object System.Drawing.Size(260,20)
ForEach ($RankDrop in $DropDownArrayRank){
$DropDownRank.Items.Add($RankDrop)
}$objForm.Controls.Add($DropDownRank)}
ElseIf ($DropDownTitle.Text -eq “Title3”){
$DropDownLabelRank = New-Object System.Windows.Forms.Label
$DropDownLabelRank.Location = New-Object System.Drawing.Size(10,220)
$DropDownLabelRank.Size = New-Object System.Drawing.Size(280,20)
$DropDownLabelRank.Text = “Please Select the SomethingMore from the Drop Down List: ”
$objForm.Controls.Add($DropDownLabelRank)
[array]$DropDownArrayRank = “SomethingMore1”, “SomethingMore2”, “SomethingMore3”
$DropDownRank = New-Object System.Windows.Forms.ComboBox
$DropDownRank.Location = New-Object System.Drawing.Size(10,240)
$DropDownRank.Size = New-Object System.Drawing.Size(260,20)
ForEach ($RankDrop in $DropDownArrayRank){
$DropDownRank.Items.Add($RankDrop)
}$objForm.Controls.Add($DropDownRank)}
})
Hey very nice blog!
Just wish to say your article is as amazing. The clarity in your post is
simply cool and i can assume you’re an expert on this subject.
Well with your permission let me to grab your RSS feed to keep updated with forthcoming post.
Thanks a million and please keep up the enjoyable work.
Excellent blog here! Also your website loads up fast! What host
are you using? Should I buy your affiliate connect to your host?
I wish my website loaded as quickly as yours lol
I am genuinely delighted to glance at this website posts which carries tons of useful data,
thanks for providing these kinds of data.
I am actually pleased to glance at this weblog posts which consists of lots
of helpful data, thanks for providing these information.
you are really a good webmaster. The web
site loading velocity is amazing. It sort of feels that
you’re doing any unique trick. Also, The contents are masterwork.
you have performed a excellent job on this topic!
This info is worth everyone’s attention. How can I find
out more?
hi!,I like your writing so so much! proportion we communicate more about your post on AOL?
I require a specialist in this house to resolve my problem.
Maybe that’s you! Having a look forward to look you.
Do you have a spam issue on this site; I also am a blogger, and I
was wondering your situation; we have created some nice practices and
we are looking to trade methods with others, please shoot me an e-mail if
interested.
Hello! This post couldn’t be written any better!
Reading through this post reminds me of my previous room mate!
He always kept talking about this. I will forward this page to
him. Pretty sure he will have a good read. Thanks for sharing!