如何知道是否同时选中了复选框

时间:2017-01-03 11:52:22

标签: wpf

我正在制作一个wpf表单,其中有一个问题,用户将选择是或否复选框。现在我需要设置一些条件,以便:

1. User has to select YES or NO, which means the question cannot be left empty
2. User cannot select both YES or NO, which means only YES or NO has to be selected
3. If user selects YES, then save the option and proceed further
4. If user selects NO, then save the option and proceed further

现在我正在做的是:

//Just for the tesing purpose, I am using message box to show what user has selected

//If user has selected YES
if (yes.IsChecked == true)
{
     MessageBox.Show(" Yes checked");
}
//If user has selected NO
if (no.IsChecked == true)
{
     MessageBox.Show("No checked");
}
//If user has not selected any option
if (yes.IsChecked == false && no.IsChecked == false)
{
      MessageBox.Show("please select option");
}
//If user has selected both the options
if (yes.IsChecked == true && no.IsChecked == true)
{
     MessageBox.Show("cannot select both options");
}

这不能按预期工作,因为如果用户同时选中这两个复选框,则会显示

Yes checked, then
No checked, then
cannot select both options

这是完成上述条件的最佳方法。

4 个答案:

答案 0 :(得分:2)

在OR情况下,您应该使用单选按钮而不是复选框。它使您无需编写自己的验证方法来检查是否一次只能选择一个,然后您只需检查已选择的内容并保存该选择。

答案 1 :(得分:0)

如果选中一个框,你可以强行清除其他选项吗?

def evaluateTree[A](tree: Tree[A]): A = {
  @tailrec
  def evaluateWhile[C](l: List[Function[C]], arguments: List[List[C]], n_args: List[Int], f: Int => Boolean, acc: C): (List[Function[C]], List[List[C]], List[Int]) =
    n_args match {
      case h :: t if f(h) =>
        evaluateWhile(l.tail, arguments.tail, n_args.tail, f, l.head.operator(arguments.head ::: List(acc)))
      case h :: t  =>
        (l, (List(acc) ::: arguments.head) :: arguments.tail,  List(n_args.head - 1) ::: n_args.tail)
      case _ =>
        (l, List(acc) :: arguments, n_args)
    }
  @tailrec
  def DFS(toVisit: List[Tree[A]], visited: List[String] = Nil, operators: List[Function[A]] = Nil, arguments: List[List[A]] = Nil, n_args: List[Int] = Nil, debug: Int = 0): A = toVisit match {
    case Leaf(id, terminal) :: tail if !visited.contains(id) => {
      val (operators_to_pass, args_to_pass, n_args_to_pass) =
        evaluateWhile[A](operators, arguments, n_args, x => x == 1, terminal.value)
      DFS(toVisit.tail, visited ::: List(id), operators_to_pass, args_to_pass, n_args_to_pass, debug + 1)
    }
    case Node(id, op, args @_*) :: tail if !visited.contains(id) => {
      DFS(args.toList ::: toVisit.tail, visited ::: List(id), op :: operators, List(Nil) ::: arguments, List(args.length ) ::: n_args, debug + 1)
    }
    case _ => arguments.flatten.head
  }
  DFS(List(tree))
}

答案 2 :(得分:0)

您可以将测试与^运算符结合使用:

if (yes.IsChecked ^ no.IsChecked)
{
    // proceed, as exactly one is checked
}

您可以选择是否检查两者:

if (yes.IsChecked && no.IsChecked)
{
    MessageBox.Show("cannot select both options");
}

作为最后一个选项,您可以测试两者是否未选中:

if (!yes.IsChecked && !no.IsChecked)
{
    MessageBox.Show("please select option");
}

答案 3 :(得分:0)

您只需重新排列if条件

 //If user has not selected any option
 if (yes.IsChecked == false && no.IsChecked == false)
 {
   MessageBox.Show("please select option");
 }
 //If user has selected both the options
 else if (yes.IsChecked == true && no.IsChecked == true)
 {
      MessageBox.Show("cannot select both options");
 }
 //If user has selected YES
 else if (yes.IsChecked == true)
 {
    MessageBox.Show(" Yes checked");
 }
 //If user has selected NO
 else if (no.IsChecked == true)
 {
   MessageBox.Show("No checked");
 }
相关问题