0

I have 7 checkboxes. What I want is to make one string for each of those checkboxs. Meaning if I had....

Orange apple pear plum grape tiger red

And orange pear and red where checked.

I'd get a string that produced "orange ; pear ; red"

1
  • 1
    What framework? ASP.NET Forms, MVC, WinForms? Commented Feb 4, 2010 at 14:23

5 Answers 5

3

What is the problem, do you need just this?

StringBuilder sb = new StringBuilder();
foreach(var cb in checkBoxes)
{
    if(cb.IsChecked)
    { 
        sb.Append(cb.Text);
        sb.Append(';');
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Good, but you might want to add a line of code in there to trim off the trailing semi-colon.
ArsenMkrt, you beat me for a few seconds. in addition, I we need to produce a string that is not like the name, we can use the Tag attribute of the checkbox to store the string.
Than you can use Tag, but I think the Text is exactly what he want, in wonforms, and Content.ToString() in wpf, just because I don't know is it wpf project or winform, I used name, I will change it now
and what is the reason of down voting?
still no reason to down vote i, simple change sb.Append(';'); to sb.Append(' ;');
|
3

What I usually do when wanting to concat strings with a given separator, is putting my strings in a string array, then use the String.Join method. Example :

string.Join(";", new string[] { "test1", "test2", "test3" }); // Which outputs test1;test2;test3

Comments

2

You could use something similar to:

List<CheckBox> boxes;
String result = String.Join(" ; ", boxes.Where(box => box.Checked)
                                        .Select(box => box.Text).ToArray());

Comments

1
var values = (from c in new[] { c1, c2, c3, c4, c5, c6, c7 }
              where c.Checked
              select c.Text).ToArray();
var result = string.Join(";", values);

Comments

0

You didn't specify WinForms versus WPF; I will assume WinForms but code is nearly identical for WPF (replace Checked by IsChecked and Text by Tag). The CheckBox control has a Checked property indicating whether or not the CheckBox is in the checked state. So say that you CheckBoxes are in an array CheckBox[] checkBoxes. Then you could say

List<string> checkedItems = new List<string>();
for(int i = 0; i < checkBoxes.Length; i++) {
    CheckBox checkBox = checkBoxes[i];
    if(checkBox.Checked) {
        checkedItems.Add(checkBox.Text);
    }
}
string result = String.Join(" ; ", checkedItems.ToArray());

Of course, that imperative and yucky. Let's get happy with some nice declarative code in LINQ:

string result = String.Join(
                     " ; ", 
                     checkBoxes.Where(cb => cb.Checked)
                               .Select(cb => cb.Text)
                               .ToArray()
                );

If your CheckBoxes are not in array, you could start by putting them in array via

CheckBox[] checkBoxes = new[] { c1, c2, c3, c4, c5, c6, c7 };

where c1, c2, ..., c7 are your CheckBoxes.

Comments

Your Answer

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