2

I'm brain tired and stumped so wanted to see if anyone knows a better way to do this:

I have 4 checkboxes that a user can select none, one, or all, or any combo. Then in the code behind I want to check all the CheckBoxes and if it's been selected then take it's value (text) and assign it to 1 string and separate it with a comma as necessary.

This works great but assigns all the .Text whether or not the items have been checked.

 if (CheckBox1.Checked || CheckBox2.Checked || CheckBox3.Checked || CheckBox4.Checked)
    {
      ClientString = CheckBox1.Text + ", " + CheckBox2.Text + ", " + CheckBox3.Text + ", " + CheckBox4.Text; 
    }

I'm staring at it and I know the solution is really simple but I'm too tired to think clearly right now and wanted to find out what I'm doing wrong. Code is C# (ASP.NET).

How do I poll the 4 CheckBoxes and if it's checked then assign it's value to the string and ignore it if it isn't checked?

Thanks.

5 Answers 5

6

What about something like this? (not tested)

For .NET 3.5

var checkboxList = new List<CheckBox>{CheckBox1,CheckBox2,CheckBox3,CheckBox4};
var messages = checkboxList
      .Where(x => x.Checked)
      .Select(x => x.Text)
      .ToArray();
ClientString = string.Join(", ", messages);

For .NET 4.0

var checkboxList = new List<CheckBox>{CheckBox1,CheckBox2,CheckBox3,CheckBox4};
var messages = checkboxList
      .Where(x => x.Checked)
      .Select(x => x.Text);
ClientString = string.Join(", ", messages);
Sign up to request clarification or add additional context in comments.

8 Comments

In your third line I don't believe it's necessary to call .ToArray() Join should be able to implicitly convert from an IEnumerable.
@MBirchmeier I too was under the impression you needed .ToArray() for .Join. I wonder where Claudio and I both came up with that. Here's a link to an answer about it: stackoverflow.com/a/122760/61654.
@MBirchmeier: I tested it on 3.5 and IEnumerable doesn't work. Am I missing something? (I do see an override accepting IEnumerable for 4.0)
@ClaudioRedi 4.0 does have the override and I believe it was new at that time.
@DavidBrainer-Banker thanks for the insight. Amazing how you fall into a particular way of doing things and miss new stuff when it comes out.
|
3

There are a few options the most brute force way is with a series of if statements.

// this runs the risk of a hanging chad
// i.e. ", value, value"
ClientString = String.Empty;

if (CheckBox1.Checked)
   ClientString = CheckBox1.Text;
if (CheckBox2.Checked)
   ClientString += ", " + CheckBox2.Text;
if (CheckBox3.Checked)
   ClientString += ", " + CheckBox3.Text;
if (CheckBox4.Checked)
   ClientString += ", " + CheckBox4.Text;

That said you might be better off using a CheckBoxList and a lambda expression.

ClientString = checkboxBoxList.Items.Cast<ListItem>()
               .Where(i => i.Selected)
               .Join(", ", i => i.Text);

For .NET 2.0 something like this would probably be best:

List<string> cbTexts = new List<string>();

if (CheckBox1.Checked)
   cbText.Add(CheckBox1.Text);
if (CheckBox2.Checked)
   cbText.Add(CheckBox2.Text);
if (CheckBox3.Checked)
   cbText.Add(CheckBox3.Text);
if (CheckBox4.Checked)
   cbText.Add(CheckBox4.Text);

string ClientString = String.Empty;

foreach (string cbText in cbTexts)
{
  ClientString += cbText ", ";
}

ClientString.Remove(ClientString.Length - 2); // remove trailing comma

4 Comments

Ended up using this option since it's a pretty simple form and I'm not actually using a checkboxlist for this. It works for the quick form I had to get going today. Thanks!
@Valien The one issue with the first method here (the one your chose) is that you could end up , value, value, value when CheckBox1 is not checked. Something to consider.
@DavidBrainer-Banker you're right. the , does show in the table but not too concerned since it's a quick/dirty table to gather data over a short period of time. I'll probably manually strip it out when I dump the contents to a spreadsheet.
@Valien I edited my answer yesterday to include something a bit cleaner.
1

You can do it like this

//clear the string 
String ClientString = String.Empty;
     if (CheckBox1.Checked )
        ClientString = CheckBox1.Text
     if (CheckBox2.Checked )
        ClientString += ", " + CheckBox2.Text;
     if (CheckBox3.Checked )
        ClientString += ", " + CheckBox3.Text;
     if (CheckBox4.Checked )
        ClientString += ", " + CheckBox4.Text;

What your code below doing is if any checkbox is checked then combine text value of all the checkboxes

if (CheckBox1.Checked || CheckBox2.Checked || CheckBox3.Checked || CheckBox4.Checked)
    {
      ClientString = CheckBox1.Text + ", " + CheckBox2.Text + ", " + CheckBox3.Text + ", " + CheckBox4.Text; 
    }

Comments

1

Okay, I read your question as asking for the value of all of the checkboxes if they were all selected and based on this I said: You wrote this with OR when I think you meant to use AND

if (CheckBox1.Checked && CheckBox2.Checked && CheckBox3.Checked && CheckBox4.Checked)
{
    ClientString = String.Format("{0}, {1}, {2}, {3}", CheckBox1.Text, CheckBox2.Text, CheckBox3.Text, CheckBox4.Text); 
}

However, now I realize that what you were really looking for was only the value of those checkboxes which were selected and you wanted it whether or not they were all selected. You might do this by concatenating a list:

List<String> values = new List<String>();
if (CheckBox1.Checked) result.Add(CheckBox1.Text);
if (CheckBox2.Checked) result.Add(CheckBox2.Text);
if (CheckBox3.Checked) result.Add(CheckBox3.Text);
if (CheckBox4.Checked) result.Add(CheckBox4.Text);
String result = String.Join(", ", values);

Comments

1

To comma separate the list:

TextList textlist = new List<string>();
if (CheckBox1.Checked) textlist.Add(CheckBox1.Text);
if (CheckBox2.Checked) textlist.Add(CheckBox2.Text);
if (CheckBox3.Checked) textlist.Add(CheckBox3.Text);
if (CheckBox4.Checked) textlist.Add(CheckBox4.Text);
ClientString clientString = string.Join(",", textlist);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.