Link to home
Start Free TrialLog in
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
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Open in new window

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
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;
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of wiggy353
wiggy353

ASKER

Perfect. Again. You always bail me out. Thanks!
NP. Glad to help  = )