Powershell从ComboBox刷新CheckBox数组

时间:2017-01-20 21:31:09

标签: arrays powershell checkbox combobox

使用Powershell,我有一个包含多个标签的表单。

在第三个选项卡中,我创建了一个从Windows注册表中选取的数字数组,我将其放入ComboBox中。 然后我根据ComboBox自动根据注册表值名称制作几个CheckBox。

我的问题是,当我更改ComboBox的文本时,我无法刷新/更新CheckBox。我尝试了“.Refresh()”,如下面和其他东西,但它不起作用。

以下是我的注册表项:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\MySoftware]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName4"=""
"ValueName5"=""
"ValueName6"=""
"ValueName8"=""
"ValueName9"=""
"ValueName10"=""
"ValueName11"=""

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder1]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder1\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName6"=""
"ValueName7"=""
"ValueName8"=""
"ValueName9"=""
"ValueName11"=""

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder10]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder10\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName6"=""
"ValueName7"=""
"ValueName8"=""
"ValueName9"=""
"ValueName10"=""
"ValueName11"=""

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder2]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder2\SubFolder]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder3]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder3\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName3"=""
"ValueName5"=""
"ValueName6"=""
"ValueName7"=""
"ValueName9"=""
"ValueName10"=""

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder4]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder4\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName4"=""
"ValueName5"=""
"ValueName6"=""
"ValueName7"=""
"ValueName10"=""
"ValueName11"=""

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder5]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder5\SubFolder]
"ValueName1"=""
"ValueName4"=""
"ValueName5"=""
"ValueName6"=""
"ValueName8"=""
"ValueName9"=""
"ValueName10"=""

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder6]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder6\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName5"=""
"ValueName6"=""
"ValueName7"=""
"ValueName9"=""
"ValueName11"=""

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder7]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder7\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName5"=""
"ValueName6"=""
"ValueName7"=""
"ValueName10"=""
"ValueName11"=""

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder8]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder8\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName3"=""
"ValueName4"=""
"ValueName7"=""
"ValueName8"=""
"ValueName9"=""
"ValueName11"=""

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder9]

[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder9\SubFolder]
"ValueName1"=""
"ValueName5"=""
"ValueName6"=""
"ValueName10"=""
"ValueName11"=""

这是我的剧本:

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

$form1 = New-Object System.Windows.Forms.Form
$tabControl = New-Object System.Windows.Forms.tabControl
$ToolTip = New-Object System.Windows.Forms.ToolTip
$ToolTip.BackColor = [System.Drawing.Color]::LightGoldenrodYellow
$ToolTip.IsBalloon = $true
    $Tab1 = New-Object System.Windows.Forms.TabPage
    $Tab2 = New-Object System.Windows.Forms.TabPage
    $tabTab3 = New-Object System.Windows.Forms.TabPage
        $labelDossierTab3 = New-Object System.Windows.Forms.Label
        $ComboTab3 = New-Object System.Windows.Forms.ComboBox
        $checkBoxAllTab3 = New-Object System.Windows.Forms.Button
        $checkBoxNothingTab3 = New-Object System.Windows.Forms.Button
        $ActiveTab3 = New-Object System.Windows.Forms.CheckBox
        $GenerateButtonTab3 = New-Object System.Windows.Forms.Button
$CloseButton = New-Object System.Windows.Forms.Button
$fontDialog1 = New-Object System.Windows.Forms.FontDialog
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

$CloseButton_OnClick=
{
$form1.Close()
}

$OnLoadForm_StateCorrection=
{
$form1.WindowState = $InitialFormWindowState
}

$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 464
$System_Drawing_Size.Width = 704
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Task generator"
$form1.StartPosition = "CenterScreen"
$form1.CancelButton = $CloseButton

$tabControl.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 136
$System_Drawing_Point.Y = 83
$tabControl.Location = $System_Drawing_Point
$tabControl.Name = "tabControl"
$tabControl.SelectedIndex = 0
$tabControl.ShowToolTips = $True
$tabControl.Multiline = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 320
$System_Drawing_Size.Width = 453
$tabControl.Size = $System_Drawing_Size
$tabControl.TabIndex = 4

$form1.Controls.Add($tabControl)

$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 4
$System_Drawing_Point.Y = 22
$Tab1.Location = $System_Drawing_Point
$Tab1.Name = "Tab1"
$System_Windows_Forms_Padding = New-Object System.Windows.Forms.Padding
$System_Windows_Forms_Padding.All = 3
$System_Windows_Forms_Padding.Bottom = 3
$System_Windows_Forms_Padding.Left = 3
$System_Windows_Forms_Padding.Right = 3
$System_Windows_Forms_Padding.Top = 3
$Tab1.Padding = $System_Windows_Forms_Padding
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 274
$System_Drawing_Size.Width = 445
$Tab1.Size = $System_Drawing_Size
$Tab1.TabIndex = 0
$Tab1.Text = "Tab1"
$Tab1.UseVisualStyleBackColor = $True

$tabControl.Controls.Add($Tab1)

$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 4
$System_Drawing_Point.Y = 22
$Tab2.Location = $System_Drawing_Point
$Tab2.Name = "Tab2"
$System_Windows_Forms_Padding = New-Object System.Windows.Forms.Padding
$System_Windows_Forms_Padding.All = 3
$System_Windows_Forms_Padding.Bottom = 3
$System_Windows_Forms_Padding.Left = 3
$System_Windows_Forms_Padding.Right = 3
$System_Windows_Forms_Padding.Top = 3
$Tab2.Padding = $System_Windows_Forms_Padding
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 274
$System_Drawing_Size.Width = 445
$Tab2.Size = $System_Drawing_Size
$Tab2.TabIndex = 4
$Tab2.Text = "Tab2"
$Tab2.UseVisualStyleBackColor = $True

$tabControl.Controls.Add($Tab2)

$tabTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 4
$System_Drawing_Point.Y = 22
$tabTab3.Location = $System_Drawing_Point
$tabTab3.Name = "tabTab3"
$System_Windows_Forms_Padding = New-Object System.Windows.Forms.Padding
$System_Windows_Forms_Padding.All = 3
$System_Windows_Forms_Padding.Bottom = 3
$System_Windows_Forms_Padding.Left = 3
$System_Windows_Forms_Padding.Right = 3
$System_Windows_Forms_Padding.Top = 3
$tabTab3.Padding = $System_Windows_Forms_Padding
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 274
$System_Drawing_Size.Width = 445
$tabTab3.Size = $System_Drawing_Size
$tabTab3.TabIndex = 4
$tabTab3.Text = "Tab3"
$tabTab3.UseVisualStyleBackColor = $True

$tabControl.Controls.Add($tabTab3)

$labelDossierTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 340
$System_Drawing_Point.Y = 6
$labelDossierTab3.Location = $System_Drawing_Point
$labelDossierTab3.Name = "labeTab3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 40
$System_Drawing_Size.Width = 50
$labelDossierTab3.Size = $System_Drawing_Size
$labelDossierTab3.TabIndex = 2
$labelDossierTab3.Text = "Option :"

$tabTab3.Controls.Add($labelDossierTab3)

$ComboTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 390
$System_Drawing_Point.Y = 3
$ComboTab3.Location = $System_Drawing_Point
$ComboTab3.Name = "labeTab3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 40
$System_Drawing_Size.Width = 45
$ComboTab3.Size = $System_Drawing_Size
$ComboTab3.TabIndex = 3
$ComboTab3.BeginUpdate()
$Dossierslist = Get-ChildItem "HKCU:\Software\MySoftware\" | Where-Object {$_ -match "Folder."} | Foreach-object {$_ -replace 'HKEY_CURRENT_USER\\Software\\MySoftware\\Folder', ''}
foreach($Dossier in $Dossierslist){$ComboTab3.Items.Add($Dossier) | sort}
$ComboTab3.EndUpdate()
$ComboTab3.SelectedIndex = 0
$ComboTab3.add_TextChanged({
    $tabTab3.Refresh()
})

$tabTab3.Controls.Add($ComboTab3)

$CleDossier = "HKCU:\Software\MySoftware\Folder"+($ComboTab3.text)+"\"
$Baseslist = @()
$Baseslist += Get-ChildItem $CleDossier | Where-Object {$_.Name -match "SubFolder"}
$Baseslist += Get-ChildItem "HKCU:\Software\MySoftware\Folder\" | Where-Object {$_.Name -match "SubFolder"}
$CheckBoxLabelsC = $Baseslist.Property -match "'ValueName1'|ValueName3|ValueName5|ValueName7|ValueName9|ValueName11" | sort | Select -Unique
$CheckBoxCounterC = 1

$CheckBoxesC = foreach($LabelC in $CheckBoxLabelsC) {
    $CheckBoxC = New-Object System.Windows.Forms.CheckBox
    $CheckBoxC.DataBindings.DefaultDataSourceUpdateMode = 0
    $CheckBoxC.UseVisualStyleBackColor = $True
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 130
    $System_Drawing_Size.Height = 20
    $CheckBoxC.Size = $System_Drawing_Size
    $CheckBoxC.TabIndex = 2
    $CheckBoxC.Text = $LabelC
    $System_Drawing_Point = New-Object System.Drawing.Point
    if($CheckBoxCounterC -lt 7){ 
        $System_Drawing_Point.X = 130}
    elseif($CheckBoxCounterC -ge 7){
        $System_Drawing_Point.X = 300}
    if($CheckBoxCounterC -lt 7){    
        $System_Drawing_Point.Y = 75 + (($CheckBoxCounterC - 1) * 20)}
    elseif(($CheckBoxCounterC -ge 7) -AND ($CheckBoxCounterC -lt 17)){  
        $System_Drawing_Point.Y = - 45 + (($CheckBoxCounterC - 1) * 20)}
    $CheckBoxC.Location = $System_Drawing_Point
    $CheckBoxC.Name = "CheckBoxC$CheckBoxCounterC"
    $CheckBoxC.Add_CheckStateChanged({
    foreach($CheckBoxC in $CheckBoxesC | Where-Object {$_.checked -eq $false}) {
    $GenerateButtonTab3.Enabled = $false}
    })
    $CheckBoxC.Add_CheckStateChanged({
    foreach($CheckBoxC in $CheckBoxesC | Where-Object {$_.checked -eq $true}) {
    $GenerateButtonTab3.Enabled = $true}
    })

    $tabTab3.Controls.Add($CheckBoxC)
    $CheckBoxC
    $CheckBoxCounterC++
}

$checkBoxAllTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 17
$System_Drawing_Point.Y = 55
$checkBoxAllTab3.Location = $System_Drawing_Point
$checkBoxAllTab3.Name = "checkBoxAllTab3"
$checkBoxAllTab3.Text = "All"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 60
$checkBoxAllTab3.Size = $System_Drawing_Size
$checkBoxAllTab3.TabIndex = 0
$checkBoxAllTab3.Add_Click({
    foreach($CheckBoxC in $CheckBoxesC) {
    $CheckBoxC.Checked = $true}
})

$tabTab3.Controls.Add($checkBoxAllTab3)

$checkBoxNothingTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 17
$System_Drawing_Point.Y = 85
$checkBoxNothingTab3.Location = $System_Drawing_Point
$checkBoxNothingTab3.Name = "checkBoxNothingTab3"
$checkBoxNothingTab3.Text = "Nothing"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 60
$checkBoxNothingTab3.Size = $System_Drawing_Size
$checkBoxNothingTab3.TabIndex = 0
$checkBoxNothingTab3.Add_Click({
    foreach($CheckBoxC in $CheckBoxesC) {
    $CheckBoxC.Checked = $false}
})

$tabTab3.Controls.Add($checkBoxNothingTab3)

$ActiveTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 320
$System_Drawing_Point.Y = 240
$ActiveTab3.Location = $System_Drawing_Point
$ActiveTab3.Name = "ActiveTab3"
$ActiveTab3.Text = "Desactivate"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 123
$ActiveTab3.Size = $System_Drawing_Size
$ActiveTab3.TabIndex = 12

$ToolTip.SetToolTip($ActiveTab3, "The task will be desactivated. You'll have to activate it by yourself")

$tabTab3.Controls.Add($ActiveTab3)

$handler_GenerateButtonTab3_Click=
{

}

$GenerateButtonTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 147
$System_Drawing_Point.Y = 244
$GenerateButtonTab3.Location = $System_Drawing_Point
$GenerateButtonTab3.Name = "GenerateButtonTab3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 150
$GenerateButtonTab3.Size = $System_Drawing_Size
$GenerateButtonTab3.TabIndex = 0
$GenerateButtonTab3.Text = "Generate"
$GenerateButtonTab3.Enabled = $false
$GenerateButtonTab3.UseVisualStyleBackColor = $True
$GenerateButtonTab3.add_Click($handler_GenerateButtonTab3_Click)

$tabTab3.Controls.Add($GenerateButtonTab3)

$CloseButton.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 475
$System_Drawing_Point.Y = 420
$CloseButton.Location = $System_Drawing_Point
$CloseButton.Name = "CloseButton"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$CloseButton.Size = $System_Drawing_Size
$CloseButton.TabIndex = 6
$CloseButton.Text = "Close"
$CloseButton.UseVisualStyleBackColor = $True
$CloseButton.add_Click($CloseButton_OnClick)

$form1.Controls.Add($CloseButton)

$fontDialog1.ShowHelp = $True

$InitialFormWindowState = $form1.WindowState
$form1.add_Load($OnLoadForm_StateCorrection)
$form1.ShowDialog()| Out-Null
}
GenerateForm

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我不认为你订阅了正确的活动。

你看过SelectedIndexChanged吗?

当选择发生变化时会触发,然后您可以查看ComboBox的Index属性以确定哪个项目已更改。索引为负数表示组合框中没有项目被选中。

点击事件与组合框中的选定项目无关。