0

With Powershell, I have a form with several tabs.

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.

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.

Here are my registry keys:

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"=""

And here is my script:

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

Thanks for your help!

Arnaud
  • 29
  • 6
  • 1
    Can you post a smaller example script that demonstrates the problem and doesn't involve loading data from the registry? – Simon Catlin Jan 20 '17 at 22:18
  • Hard to do a smaller script because the checkbox array involve the the tab, the two buttons to select all checkboxes or nothing, the combobox, and the content of the registry. I tell you that because I succeed to update the checkboxes with the combobox by including the checkbox array in the combobox.Add_TextChanged({}). But the two buttons doesn't work anymore, and the reg value names are added not replaced. For the registry, it's just look like : Folder1 SubFolder RegValue1 ... RegValue11 Folder2 SubFolder RegValue1 ... RegValue11 Folder3 ... – Arnaud Jan 21 '17 at 10:52
  • Debug it: use PowerShell ISE to set breakpoints in the code, then inspect the objects and variables. – wOxxOm Jan 21 '17 at 19:11

1 Answers1

0

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

Have you looked at SelectedIndexChanged?

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.

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

PowerMapi
  • 29
  • 2
  • I still have the same issue. The Selectedindex of the combobox is updated, but I still don't know to update the checkboxes. – Arnaud Jan 23 '17 at 15:29
  • ah, ok. i *think* that you need to cast the item in the combobox to a checkbox so that you can then access the .Checked property. So, [System.Windows.Forms.Checkbox]$combobox.items(index) should cast the item returned to a checkbox. – PowerMapi Jan 24 '17 at 02:46
  • Sorry, i don't know what you mean exactly (I'm more in system&network than development). Could you please show me more? – Arnaud Jan 24 '17 at 10:33