Dr.Bake
Dr.Bake

Reputation: 151

How to check if multiple checkboxes are checked

So in my program, i have three checboxes (A, B and C). and I want to save the content of the checkbox the is checked to a text file. I am doing this using if statements as shown below:

if (a.IsChecked == true)
{
    res = a.Content.ToString() + " is checked"; 
}
else if (b.IsChecked == true)
{
    res = b.Content.ToString() + " is checked";
}
else if (c.IsChecked == true)
{
    res = c.Content.ToString() + " is checked";
}

And here is where i am saving the above values to a string and then later in my code to a text file

string test = res;

Now this is working for me. So i decided to try to check if multiple checkboxes are being checked. So added the below if statements:

else if ((a.IsChecked == true) && (b.IsChecked == true) && (c.IsChecked == true))
{
    res= a.Content.ToString() + " " + b.Content.ToString() + " " + c.Content.ToString()
}

but this isn't working with me because in the end res is printed in the text file as a rather than a b c. Any idea what i am doing wrong?

Also please note that i already initialized res at the top of my code as string:

string res;

I am not getting any error when i run my code so i am not sure where my mistake is. any help with this is much much appreciated.

thanks a lot :)

Upvotes: 0

Views: 3636

Answers (3)

Sinatr
Sinatr

Reputation: 21999

That's multiple combinations to check. Simply remove else from first code snippet to run all checks one after another. You will get only report from last successful check, to have several reports you have to accumulate them somehow (add to a list, combine in multi-line string, etc.).

Here is a simple one-liner (using linq):

var result = string.Join(" and ", new[] { a, b, c }.Where(o => o.IsChecked).Select(o => $"{o.Content} is checked"));

Upvotes: 0

rmojab63
rmojab63

Reputation: 3631

Its a good practice to use a StringBuilder in these cases. On the other hand, if it is ok to have one line for each CheckBox, you can use the following:

 StringBuilder sb = new StringBuilder();
 checkappend(ref sb, a);
 checkappend(ref sb, b);
 checkappend(ref sb, c);
 string res = sb.ToString();

in which

static void checkappend(ref StringBuilder sb, CheckBox ck)
{
    sb.Append(ck.Content.ToString());
    sb.Append(ck.IsChecked == true ?  "is checked." : " is NOT checked.");
    sb.Append(Environment.NewLine);
}

Note that creating a separate class can help you when there are many CheckBoxes in a List. You can simply use

   foreach (var ck in listOfCheckBoxes)
       checkappend(ref ck, c);

Upvotes: 1

RusArt
RusArt

Reputation: 576

You can implement it this way:

string res = "";
if (a.IsChecked)
{
    res += a.Content.ToString(); 
}
if (b.IsChecked)
{
    res += b.Content.ToString();
}
if (c.IsChecked)
{
    res += c.Content.ToString();
}

or simple

string res = $"{(a.IsChecked?a.Content+" ":"")}{(b.IsChecked?b.Content+" ":"")}{(c.IsChecked?c.Content:"")}";

Upvotes: 0

Related Questions