How do I dynamically create check boxes from an array using the form?

前端 未结 1 1685
悲哀的现实
悲哀的现实 2021-01-24 20:29

I would like to use the code to dynamically create check boxes based on an array or object I pass to the function. Can you revise this function to take an array? I have a script

1条回答
  •  终归单人心
    2021-01-24 21:15

    To do this, first we need to add a param block to the function to accept the input:

    function GenerateForm {
        param(
            [string[]]$CheckBoxLabels
        )
       # Rest of code goes here ...
    }
    

    Then, remove all existing references to $checkBox1 through 3, and let's start building those based on the parameter instead:

    # Keep track of number of checkboxes
    $CheckBoxCounter = 1
    
    # When we create a new textbox, we add it to an array for easy reference later
    $CheckBoxes = foreach($Label in $CheckBoxLabels) {
        $CheckBox = New-Object System.Windows.Forms.CheckBox        
        $CheckBox.UseVisualStyleBackColor = $True
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Width = 104
        $System_Drawing_Size.Height = 24
        $CheckBox.Size = $System_Drawing_Size
        $CheckBox.TabIndex = 2
    
        # Assign text based on the input
        $CheckBox.Text = $Label
    
        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 27
        # Make sure to vertically space them dynamically, counter comes in handy
        $System_Drawing_Point.Y = 13 + (($CheckBoxCounter - 1) * 31)
        $CheckBox.Location = $System_Drawing_Point
        $CheckBox.DataBindings.DefaultDataSourceUpdateMode = 0
    
        # Give it a unique name based on our counter
        $CheckBox.Name = "CheckBox$CheckBoxCounter"
    
        # Add it to the form
        $form1.Controls.Add($CheckBox)
        # return object ref to array
        $CheckBox
        # increment our counter
        $CheckBoxCounter++
    }
    

    Now, all we need to do is change the Click event handler to loop over the array instead of referencing individual checkboxes:

    $handler_button1_Click= 
    {
        $listBox1.Items.Clear();
    
        # Keep track of whether something has been added to the list
        $ContentPresent = $false
        # Iterate over the checkboxes, one by one
        foreach($CheckBox in $CheckBoxes){
            if($CheckBox.Checked){
                $listBox1.Items.Add("{0} (with value ""{1}"") has been checked" -f ($CheckBox.Name,$CheckBox.Text))
                $ContentPresent = $True
            }
        }
    
        # If something was already added to the list, no need to show default message
        if (-not $ContentPresent) { $listBox1.Items.Add("No CheckBox selected....") } 
    }
    

    With Computer names:

    With Animal names:

    You could sand the edges further by setting the location and size of other controls based on the number of checkboxes you have

    0 讨论(0)
提交回复
热议问题