Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top