简体   繁体   中英

Checking and assigning Checkbox values to a string in C#

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.

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);

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

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);

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; 
    }

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM