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.
This topic is 5 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
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Save Checkbox state in a file

Post by derhoeppi »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Save Checkbox state in a file

Post by jvierra »

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
Last visit: Mon Nov 28, 2022 8:48 am

Re: Save Checkbox state in a file

Post by derhoeppi »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Save Checkbox state in a file

Post by jvierra »

Save it like this:

Code: Select all

$form.Controls | Where{ $_.GetType().Name -eq 'CheckBox'} | Select Name, Text, @{n='Checked';e={[int]$_.Checked}}
This topic is 5 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