如何使用表单从数组动态创建复选框?

Posted

技术标签:

【中文标题】如何使用表单从数组动态创建复选框?【英文标题】:How do I dynamically create check boxes from an array using the form? 【发布时间】:2015-11-23 13:26:52 【问题描述】:

我想使用代码根据传递给函数的数组或对象动态创建复选框。你能修改这个函数来获取一个数组吗?我有一个脚本,可以根据用户名查找可能的 PC 名称并列出匹配项。如果有这个表格让我可以选择列表中的一个结果作为正确的 PC 移动到正确的容器中并安装软件,那就太好了。

function GenerateForm 

[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$listBox1 = New-Object System.Windows.Forms.ListBox
$checkBox3 = New-Object System.Windows.Forms.CheckBox
$checkBox2 = New-Object System.Windows.Forms.CheckBox
$checkBox1 = New-Object System.Windows.Forms.CheckBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

$b1= $false
$b2= $false
$b3= $false

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------

$handler_button1_Click= 

    $listBox1.Items.Clear();    

    if ($checkBox1.Checked)       $listBox1.Items.Add( "Checkbox 1 is checked"  ) 

    if ($checkBox2.Checked)      $listBox1.Items.Add( "Checkbox 2 is checked"  ) 

    if ($checkBox3.Checked)      $listBox1.Items.Add( "Checkbox 3 is checked"  ) 

    if ( !$checkBox1.Checked -and !$checkBox2.Checked -and !$checkBox3.Checked )    $listBox1.Items.Add("No CheckBox selected....") 


$OnLoadForm_StateCorrection=
#Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState


#----------------------------------------------
#region Generated Form Code
$form1.Text = "Primal Form"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 450
$System_Drawing_Size.Height = 236
$form1.ClientSize = $System_Drawing_Size

$button1.TabIndex = 4
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True

$button1.Text = "Run Script"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 27
$System_Drawing_Point.Y = 156
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($handler_button1_Click)

$form1.Controls.Add($button1)

$listBox1.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 301
$System_Drawing_Size.Height = 212
$listBox1.Size = $System_Drawing_Size
$listBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$listBox1.Name = "listBox1"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 137
$System_Drawing_Point.Y = 13
$listBox1.Location = $System_Drawing_Point
$listBox1.TabIndex = 3

$form1.Controls.Add($listBox1)


$checkBox3.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104
$System_Drawing_Size.Height = 24
$checkBox3.Size = $System_Drawing_Size
$checkBox3.TabIndex = 2
$checkBox3.Text = "CheckBox 3"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 27
$System_Drawing_Point.Y = 75
$checkBox3.Location = $System_Drawing_Point
$checkBox3.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox3.Name = "checkBox3"

$form1.Controls.Add($checkBox3)


$checkBox2.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104
$System_Drawing_Size.Height = 24
$checkBox2.Size = $System_Drawing_Size
$checkBox2.TabIndex = 1
$checkBox2.Text = "CheckBox 2"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 27
$System_Drawing_Point.Y = 44
$checkBox2.Location = $System_Drawing_Point
$checkBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox2.Name = "checkBox2"

$form1.Controls.Add($checkBox2)



    $checkBox1.UseVisualStyleBackColor = $True
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 104
    $System_Drawing_Size.Height = 24
    $checkBox1.Size = $System_Drawing_Size
    $checkBox1.TabIndex = 0
    $checkBox1.Text = "CheckBox 1"
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 27
    $System_Drawing_Point.Y = 13
    $checkBox1.Location = $System_Drawing_Point
    $checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0
    $checkBox1.Name = "checkBox1"

$form1.Controls.Add($checkBox1)


#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

 #End Function

#Call the Function
GenerateForm

【问题讨论】:

【参考方案1】:

为此,首先我们需要在函数中添加一个param 块来接受输入:

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

然后,删除对$checkBox13 的所有现有引用,让我们开始基于参数构建这些引用:

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

现在,我们需要做的就是更改 Click 事件处理程序以循环遍历数组而不是引用单个复选框:

$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....")  

使用计算机名称:

动物名称:

您可以通过根据您拥有的复选框数量设置其他控件的位置和大小来进一步打磨边缘

【讨论】:

刚看到这个,马上去测试。这看起来很棒! 如何返回所选内容的值? 当我选择“运行脚本”时,该框保持打开状态如何继续运行脚本? 请教各位,如何将选定的值返回给调用 PS 脚本?

以上是关于如何使用表单从数组动态创建复选框?的主要内容,如果未能解决你的问题,请参考以下文章

如何为动态字段Angular 4创建表单生成器?

如何从数组创建动态数组

如何使用 jquery 创建具有来自动态创建的表单字段的值的多维数组?

使用动态创建的复选框和数组维护复选框值

动态创建复选框

Django 动态表单,带有 HTML 数组的表单集