Save Checkbox state in a file

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
Locked
User avatar
derhoeppi
Posts: 34
Joined: Sun Apr 06, 2014 11:10 pm

Save Checkbox state in a file

Post by derhoeppi » Fri Aug 31, 2018 1:17 am

Hi,
if a user run's my GUI Project it should be possible that the user change some preferences. Therefore i create an empty form where i placed some checkboxes and two buttons (Apply / Close). The apply button should call an event or function to collect the state of each checkbox an write them in a file. At the startup of running the Project it should be read in from the file.
Now my problem is to collect all states of the checkboxes without the need to call the checkbox element direct. In my opinion it should be possible to ask the running project for all checkbox elements in it's form to create a loop that save the checkbox state in a file. Same one to read the file an set the checkbox state at startup.

User avatar
jvierra
Posts: 13507
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Save Checkbox state in a file

Post by jvierra » Fri Aug 31, 2018 1:41 am

Just enumerate the controls on the form and filter for checkboxes.

$form.Controls | Where{ $_.GetType().Name -eq 'CheckBox'} | Select Name, Text, Checked

User avatar
derhoeppi
Posts: 34
Joined: Sun Apr 06, 2014 11:10 pm

Re: Save Checkbox state in a file

Post by derhoeppi » Tue Sep 11, 2018 5:23 am

Hi

Code: Select all

$form.Controls | Where{ $_.GetType().Name -eq 'CheckBox'} | Select Name, Text, Checked
this one i save it in an array and then in a file. If i open the GUI i want to load the file and set the checkbox checked state.

Now my problem is that i read in the file and my array says that checked is a System.String. If i say it should be bool - no change will be done. So i could not set the checkbox state like $checkbox1.checked = $true. With my problem it would be say $checkbox1.checken = true. Is it possible to convert it from System.String to Boolean.

User avatar
jvierra
Posts: 13507
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Save Checkbox state in a file

Post by jvierra » Tue Sep 11, 2018 5:38 am

Save it like this:

Code: Select all

$form.Controls | Where{ $_.GetType().Name -eq 'CheckBox'} | Select Name, Text, @{n='Checked';e={[int]$_.Checked}}

Locked