Reputation: 63
How can I check for 2 or more checkBox IsChecked values inside single if statement in WPF C# ? I am trying to do like this but its not working.
if ((symbols_cb.IsChecked ?? true) && (digits_cb.IsChecked ?? true))
{
MessageBox.Show("Check - Yes it is selected.");
}
Here symbols_cb and digits_cb are check box and I want a message to be displayed while both the checkbox has been selected.
Upvotes: 1
Views: 1447
Reputation: 393
the operator ??
not work as you want, this operator chek if left value is null and it is null return rigth value.
you need do this:
if (symbols_cb.IsChecked && digits_cb.IsChecked)
{
MessageBox.Show("Check - Yes it is selected.");
}
Edit: i did´t know that in WPF isChecked return bool?
. try check value after check if IsChecked has value:
if (symbols_cb.IsChecked.HasValue ?? symbols_cb.IsChecked : false &&
digits_cb.IsChecked.HasValue ?? digits_cb.IsChecked : false )
{
MessageBox.Show("Check - Yes it is selected.");
}
Upvotes: 1
Reputation: 3631
if this is your View:
<StackPanel>
<CheckBox Name="symbols_cb" Checked="checkBox_CheckedChanged" Unchecked="checkBox_CheckedChanged" />
<CheckBox Name="digits_cb" Checked="checkBox_CheckedChanged" Unchecked="checkBox_CheckedChanged"/>
</StackPanel>
you can handle the events in the following method:
private void checkBox_CheckedChanged(object sender, RoutedEventArgs e)
{
if ((symbols_cb.IsChecked == true) && (digits_cb.IsChecked == true))
{
MessageBox.Show("Check - Yes it is selected.");
}
}
Upvotes: 1
Reputation: 543
Check this answer:
if ((symbols_cb.IsChecked ?? false) && (digits_cb.IsChecked ?? false))
{
MessageBox.Show("Check - Yes it is selected.");
}
Upvotes: 1