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.

Valien
  • 1,125
  • 2
  • 17
  • 38

5 Answers5

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);
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • 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 Dec 21 '11 at 20:23
  • @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: http://stackoverflow.com/a/122760/61654. – ahsteele Dec 21 '11 at 20:24
  • @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) – Claudio Redi Dec 21 '11 at 20:27
  • @ClaudioRedi 4.0 does have the override and I believe it was new at that time. – David Brainer Dec 21 '11 at 20:28
  • @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. – ahsteele Dec 21 '11 at 20:37
  • This looks pretty cool. Having some weird errors, maybe because the app/site is built on .NET 2.0 framework? (yeah..legacy stuff I've inherited). – Valien Dec 21 '11 at 20:38
  • I'm going to study this a little more. For my quick form I used the multiple loops. Might not be the cleanest but for this simple form it worked and was quick. I'm still a C# newbie so this solution looks pretty cool but kept throwing errors on my build. – Valien Dec 21 '11 at 20:51
  • @ahsteele believe me, changes to the framework often catch me by surprise as well - especially small conveniences like this – David Brainer Dec 21 '11 at 20:53
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
ahsteele
  • 26,243
  • 28
  • 134
  • 248
  • 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 Dec 21 '11 at 20:49
  • @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. – David Brainer Dec 21 '11 at 20:57
  • @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 Dec 22 '11 at 20:20
  • @Valien I edited my answer yesterday to include something a bit cleaner. – ahsteele Dec 23 '11 at 06:02
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);
David Brainer
  • 6,223
  • 3
  • 18
  • 16
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; 
    }
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
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);
Jacob
  • 77,566
  • 24
  • 149
  • 228
Brandon
  • 983
  • 6
  • 15