Powershell从ComboBox刷新CheckBox数组 [英] Powershell refresh CheckBox array from ComboBox

查看:120
本文介绍了Powershell从ComboBox刷新CheckBox数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Powershell,我有一个带有多个选项卡的表单。

With Powershell, I have a form with several tabs.

在第三个选项卡中,我将输入从Windows注册表中提取的数字数组一个组合框。
然后我会根据ComboBox的注册表值名称自动制作几个CheckBox。

In the third tab, I make an array of numbers picked from Windows registry that I'll put in a ComboBox. Then I make automatically several CheckBox by registry value names depending the ComboBox.

我的问题是,当我更改复选框时,无法刷新/更新CheckBox。 ComboBox的文本。我像下面这样尝试了 .Refresh()和其他方法,但是它不起作用。

My problem is that I can't refresh/update the CheckBox when I change the text of the ComboBox. I tried ".Refresh()" like below and other things, but it doesn't work.

这是我的注册表项:

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

感谢您的帮助!

推荐答案

我认为您没有参加正确的活动。

I don't think you're subscribing to the right event.

您是否查看过 SelectedIndexChanged

这将在选择更改时触发,然后您可以查看ComboBox的Index属性来确定更改的项目。索引为负的索引表示组合框中没有项目被选中。

This will fire when a selection has changed and then you can look at the ComboBox's Index property to figure out which item changed. An index of negative one means no item in the combobox is selected.

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

The click event has nothing to do with selected items in a combobox.

这篇关于Powershell从ComboBox刷新CheckBox数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆