0

giving the following PowerShell Studio Code, how can I call and disable the 'David Checkbox' under the 'Checkit' button. I know I am messing up on the checkbox declaration somehow because powershell does not recognize my checkbox as an object:

The property 'Enabled' cannot be found on this object. Verify that the property exists and can be set.

$accounts = "David", "Dave"

$buttonLoadLabels_Click = {
    $CheckBoxCounter = 1
    $accounts = 'didier','david'

    foreach ($account in $accounts)
    {

        $label = New-Object System.Windows.Forms.Label
        $label.Text = $account
        $label.TextAlign = 'MiddleCenter'
        $label.Font = $label1.Font
        $flowlayoutpanel1.Controls.Add($label)



        $CB = New-Object System.Windows.Forms.CheckBox
        $CB.Name = $account
        Write-Host $CB.Name
        $flowlayoutpanel1.Controls.Add($CB)




    }
}



$buttonCheckIt_Click = {


    $checkbox_David.Enabled = $false

}
Theo
  • 57,719
  • 8
  • 24
  • 41
Didi
  • 1
  • Where is `$flowlayoutpanel1` defined? You are using `$CB` for the checkbox object, but in your button click handler you suddenly call it `$checkbox_David`, where it should be `$CB` with a name property 'David'. /// Also, it would have been better if you would have edited your [previous question](https://stackoverflow.com/q/58623657/9898643) instead of posting the same one (with additions) as new question. – Theo Oct 30 '19 at 13:01
  • $CB.enabled= $false (?) - how to use the 'name property'? thanks – Didi Oct 30 '19 at 13:05
  • Something like `if ($CB.Name -eq 'David') { $CB.Enabled = $false }` perhaps? Don't you actually mean `$CB.Text = $account` ? You may also want to group the checkboxes inside a [System.Windows.Forms.GroupBox](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.groupbox?view=netframework-4.8) – Theo Oct 30 '19 at 13:15
  • if ($CB.Name -eq 'david') { $CB.Enabled = $false } did not work – Didi Oct 30 '19 at 13:29
  • I am not using the Text because it does not suit my needs - – Didi Oct 30 '19 at 13:31
  • did you check the `.Enabled` property afterwards? or do you in fact want the [Checked](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.checkbox.checked?view=netframework-4.8) property instead? – Theo Oct 30 '19 at 13:31
  • For testing, I tried both Checked and Enabled in case it ws the issue - however none of them worked - but yes you are right I'm planning to use 'checked' – Didi Oct 30 '19 at 13:34
  • Is your `$buttonCheckIt` defined anywhere? Also, creating an eventhandler for a button needs `.Add_Click({...})`, not `_Click {...}`. I think you may need to read some more about [Building Forms with PowerShell](https://blogs.technet.microsoft.com/stephap/2012/04/23/building-forms-with-powershell-part-1-the-form/) – Theo Oct 30 '19 at 13:37
  • yes it is and working for other features - just cant call the checkbox - its powershell studio handler – Didi Oct 30 '19 at 13:40
  • I got it to work with GLOBAL variable. Does it makes any sence to you guys? thanks for all . – Didi Oct 30 '19 at 16:36
  • if ($GLOBAL:CB.Name -eq 'david') { $GLOBAL:CB.checked = $true } – Didi Oct 30 '19 at 16:36

0 Answers0