Uncheck all

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Uncheck all

Post by supportMIB »

I found this code of C# on the web to uncheck all checked boxes.

Is there a way to convert it to powershell?

I'm sure I can figure it all out if I could just understand how to do the checkbox chkbox = ctrl as checkbox; part and have all checkboxes assigned to a variable

private void UncheckAll(Control ctrl)
{
CheckBox chkBox = ctrl as CheckBox;
if (chkBox == null)
{
foreach (Control child in ctrl.Controls)
{
UncheckAll(child);
}
}
else
{
chkBox.Checked = false;
}
}
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Uncheck all

Post by dan.potter »

Yes, all c# can be written in powershell.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Uncheck all

Post by jvierra »

In PowerShell - same code:

$groupbox1.Controls | %{ $_.Checked = $false}
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: Uncheck all

Post by supportMIB »

Perfect, thanks!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Uncheck all

Post by jvierra »

For ungrouped controls on a form:
  1. $form1.Controls |
  2.     ForEach-Object{
  3.         if($_ -is [System.Windows.Forms.CheckBox]){
  4.             $_.Checked = $false
  5.     }
  6. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Uncheck all

Post by jvierra »

Do you really need to do this recursively?
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: Uncheck all

Post by supportMIB »

The groupbox method actually works perfectly. I have a few group boxse with about 50 checkboxes apiece. The first example is perfect!
This topic is 7 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked