Click to See Complete Forum and Search --> : Checked - true


aggror
June 30th, 2009, 04:38 AM
Hi,

I'm trying to create a simple check condition with if and else. Although it should be easy, I can't get it to work.
this is what I have:
if(checked = true)
{
panel1.Enabled = true;
}
else
{
panel1.Enabled = false;
}

jakko100
June 30th, 2009, 05:42 AM
First of all, you need to put this condition to an checkbox event CheckedChanged. If you change your checkbox checked status, then it enables/disables yor panel.

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked)
{
panel1.Enabled = true;
}
else
{
panel1.Enabled = false;
}
}

If you want it to work when application is started then you must enable/disable your panel on form Load event or put the code


panel1.Enabled = false;


to the Form constructor.

ozzy66
June 30th, 2009, 05:57 AM
if(checked = true)



if(checked == true)

or simply better

if(checked)

memeloo
June 30th, 2009, 06:07 AM
or just

panel1.Enabled = checkBox1.Checked

;]

eclipsed4utoo
June 30th, 2009, 09:39 AM
single equal sign - assignment
double equal sign - checks for equality

I also agree with memeloo. No real need for the if statement.

boudino
July 1st, 2009, 03:42 AM
Has others has pointed out, comparation is done with ==, not =. But it could be just a typo, so it would be nice if specify the error more precisely, like it is an compile-time error (which), run-time error, or it doesn't fail. but doesn't do what it should.

aggror
July 6th, 2009, 04:50 AM
okay, I've found the solution. Wasn't that hard, but when you don't know how to type it, you can be stuck for a long time..

thanks for your help
solution:

private void Checkbox1_CheckStateChanged(object sender, EventArgs e)
{
panel1.Enabled = checkBox1.Checked;
}

aggror
July 6th, 2009, 04:53 AM
for the record: by "I found it", I actually ment: I tried the solution from Memelo.