Powershell and Forms (part 2) – Multiple Dropdown lists

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:

# 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.

11 comments

  1. 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.

  2. 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

  3. I am genuinely delighted to glance at this website posts which carries tons of useful data,
    thanks for providing these kinds of data.

  4. I am actually pleased to glance at this weblog posts which consists of lots
    of helpful data, thanks for providing these information.

  5. 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!

  6. 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.

  7. 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.

  8. 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!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.