Avatar of wiggy353
wiggy353
 asked on

parse string to checkbox checkstate

I have a windows form application that calls an XML file for some of the settings. One of the settings that I want to pull in is the state of a checkbox. I have it pulling in either "Checked" or "Unchecked". However, I cannot get that value to the check box because it needs to convert from 'string' to 'System.Windows.Forms.CheckState'.

I can pull in the text from the XML and enter it into a variable called useSchedule, but then:

useScheduleCheckBox.CheckState = useSchedule;

is not working.

Any help, experts?

Thanks
.NET ProgrammingC#

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
kaufmed

Try this:
try
{
    checkBox1.CheckState = (CheckState)Enum.Parse(typeof(CheckState), "Checked");
}
catch (ArgumentException ex)
{
    MessageBox.Show(ex.ToString());
}

Open in new window

HainKurt

do this

if (useSchedule=="0") useScheduleCheckBox.CheckState  = CheckState.Unchecked;
if (useSchedule=="1") useScheduleCheckBox.CheckState  = CheckState.Checked;
if (useSchedule=="2") useScheduleCheckBox.CheckState  = CheckState.Indeterminate;

I am not sure what values do you have in useSchedule
HainKurt

check your xml file what is stored in it, based on those values modify the code above as

if (useSchedule=="???") useScheduleCheckBox.CheckState  = CheckState.Unchecked;
if (useSchedule=="???") useScheduleCheckBox.CheckState  = CheckState.Checked;
if (useSchedule=="???") useScheduleCheckBox.CheckState  = CheckState.Indeterminate;
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
kaufmed

P.S.

If you think there's a chance that the values in your config file might not be cased properly, the Parse method takes an optional 3rd parameter of type boolean which indicates whether or not to perform a case-insensitive parse.
wiggy353

ASKER
Perfect. Again. You always bail me out. Thanks!
kaufmed

NP. Glad to help  = )
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.