parse string to checkbox checkstate

wiggy353
wiggy353 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

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

Open in new window

HainKurtSr. System Analyst

Commented:
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
HainKurtSr. System Analyst

Commented:
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;
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
I missed your code snippet. This is applicable to what you posted:
try
{
    useScheduleCheckBox.CheckState = (CheckState)Enum.Parse(typeof(CheckState), useSchedule);
}
catch (ArgumentException ex)
{
    MessageBox.Show(ex.ToString());
}

Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
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.

Author

Commented:
Perfect. Again. You always bail me out. Thanks!
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
NP. Glad to help  = )

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial