If Both Checkboxes flag false do?

Go To StackoverFlow.com

1

I am getting frustrated with Google query's on code so I'll just ask all you experts.

I have a <500 line program that lists files from clearcase in a tree now I want to click on them and check them in/out. The checkout part I can accomplish via command line or plugin (::whew:: clearcase is not easy to program with) anyhow I have to checkboxes one for checkin one for checkout and if neither are selected (both false) I want it to notify the user. I have the following code and it works...except that it always displays the "not selected" message since one flag is always false.

if (checkBox1.checked == true
{
   MessageBox.Show("this")
}
if (checkBox2.checked == true 
{
   MessageBox.Show("THAT")
}
if (checkbox1.checked == false | checkbox2.checked == false)
{
   MessageBox.show("You didn't select this or THAT")
}

After the first choice it always says "THAT" or "this" then "You didn't select this or THAT"

Also I have the checkboxes setup so that when you check one it unchecks the other, but both can be false.

2012-04-03 20:09
by xSCM
Try nesting the last if statement - simchona 2012-04-03 20:11
There's no actual question here - Sam Axe 2012-04-03 20:13
and your | (or) should be an & (and - Sam Axe 2012-04-03 20:13
more likely an && (andalso - Sam Axe 2012-04-03 20:14
Hmm, why is this not a RadioButton? For that matter, how come you don't know why it is an check-in or check-out - Hans Passant 2012-04-03 20:14
One will always be false. Thus, if (false OR false) will always be true - dbooth 2012-04-03 20:15
You are right Boo, I missed the question but somehow it was answered anyway ;) I didnt want to use radio buttons because the tree view is tricky and sometimes i click the wrong branch just trying to expand the tree, this saves me the hassle of having to go back and recheck in or out if i click the wrong one first - xSCM 2012-04-03 20:36


6

You want to check that they are both false, and that is done with a logical AND. The vertical bar represents an OR condition.

if (checkbox1.checked == false && checkbox2.checked == false)
{
    MessageBox.show("You didn't select this or THAT")
}

That should do what you are looking for.

2012-04-03 20:11
by scwagner
That does it TYVM! I knew it would be something simple, I wasn't sure how to properly query that in Google so thanks for saving me many frustrating minutes of searching through irrelevant code - xSCM 2012-04-03 20:15


1

Try

if (checkBox1.checked == true)
{ 
    MessageBox.Show("this") 
} 
else if (checkBox2.checked == true)
{ 
    MessageBox.Show("THAT") 
} 
else 
{ 
    MessageBox.show("You didn't select this or THAT") 
} 

or better:

if (checkBox1.checked)
{ 
    MessageBox.Show("this") 
} 
else if (checkBox2.checked)
{ 
    MessageBox.Show("THAT") 
} 
else 
{ 
    MessageBox.show("You didn't select this or THAT") 
} 
2012-04-03 20:11
by phoog


1

Use an if-else statement instead of just an if statement. That way it will stop after executing the first statement.

2012-04-03 20:12
by user1308752


1

if (checkBox1.checked)
{
   MessageBox.Show("this")
}
else if (checkBox2.checked)
{
   MessageBox.Show("THAT")
}
else
{
   MessageBox.show("You didn't select this or THAT")
}
2012-04-03 20:12
by SwDevMan81


1

What about

(checkbox1.checked == false & checkbox2.checked == false)

The vertical line is OR while the ampersand is AND

2012-04-03 20:15
by user1308520