3

I would like to concatenate the checked values of a CheckBoxList. I am using the following code which works fine in the form's code behind:

string sFooType = ""; 

for (int i = 0; i < chkFooTypes.Items.Count; i++) 
{
    if (chkFooTypes.Items[i].Selected)
    {
        if (sFooType == "")
            sFooType = chkFooTypes.Items[i].Text;
        else
            sFooType += "," + chkFooTypes.Items[i].Text;
    } 
}

However, I would like to put this code in its own class to be called when required. The same CheckBoxList appears on two different forms - I'm trying not to repeat code. I know I'm being a bit pedantic, but its the only way to learn!

Could I populate a public list and then concatenate the list? Where I'm stumped is how the class will know which control/form to work with.

I did try to adapt this solution, but I couldn't get my head around it. I could see how it worked for textbox, but not how it would work for a CheckBoxList.

4 Answers 4

3

You can create an extension-method which works for any ListControl (like CheckBoxList, ListBox or DropDownList):

public static class ListControlExtensions
{
    public static string GetSelectedItemText(this ListControl list, string separator = ",")
    {
        return string.Join(separator, list.Items.Cast<ListItem>()
            .Where(li => li.Selected)
            .Select(li => li.Text));
    }
}

You use it in this way:

string selectedItems = chkFooTypes.GetSelectedItemText();

Note that you need to add using System.Linq;.

Sign up to request clarification or add additional context in comments.

1 Comment

This is very elegant. Don't quite understand it, but elegant all the same! Thank you.
3

You need to Pass CheckBoxList as parameter for this function

Use this

public string getstring(CheckBoxList chk)
{
    string sFooType = "";
    for (int i = 0; i <= chkFooTypes.Items.Count - 1; i++) {
        if (chkFooTypes.Items(i).Selected) {
            if (string.IsNullOrEmpty(sFooType)) {
                sFooType = chkFooTypes.Items(i).Text;
            } else {
                sFooType += "," + chkFooTypes.Items(i).Text;
            }
        }
    }
    return sFooType;
}

And you can call this function from anywhere For Example

private void Button1_Click(object sender, EventArgs e)
{
    string s = "";
    s = getstring(chkFooTypes);
}

1 Comment

Worked great for me, thanks. I did have to match the parameters up though - 'chk' is passed through, but 'chkFooTypes' is used. Great solution though!
1

Sounds like your better solution for reuse would be to have a common method that you could call and pass in the right instance of the list:

public string ConcatCheckboxlist(CheckBoxList chklist)
{
    string sRet;
    if (chklist.Items[i].Selected)
    {
        if (sRet == "")
            sRet= chklist.Items[i].Text;
        else
            sRet+= "," + chklist.Items[i].Text;
    } 
    return sRet;
}

This call this with your CheckBoxList:

string sFooType = ConcatCheckboxlist(chkFooTypes);

Comments

1

You should wrap it into a method and pass your CheckBoxList as a parameter.

public string GetConcatenation(CheckBoxList list)
{
    string value= ""; 
    for (int i = 0; i < list.Items.Count; i++)
    {
        if (list.Items[i].Selected)
        {
            if (value== "")
                value= list.Items[i].Text;
            else
                value+= "," + list.Items[i].Text;
        }
    }
    return value;
}

And then invoke the method like this:

string concatenatedValue= GetConcatenation(chkFooTypes);

To make it abstracted from a concrete control type you could pass the ListItemCollection:

public string GetConcatenation(ListItemCollection list)
{
    string value= ""; 
    for (int i = 0; i < list.Count; i++)
    {
        if (list[i].Selected)
        {
            if (value== "")
                value= list[i].Text;
            else
                value+= "," + list[i].Text;
        }
    }
    return value;
}

And then invoke the method like this:

string concatenatedValue= GetConcatenation(chkFooTypes.Items);

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.