Getting CheckBoxList Item values
Asked Answered
Z

9

11

I have a CheckBoxList which I'm populating with data. When I attempt to retrieve the checked items from the list I can only grab the item ordinal, I cannot get the value.

I've read that you can use Items[i].Value however when I try to do this I get an error stating that there is no extension method 'value'.

Here's the code I'm using to try and grab the information (note the GetItemText(i) actually only gives me the item position, not the text for the item)

   private void btnGO_Click(object sender, EventArgs e)
{
    for (int i = 0; i < chBoxListTables.Items.Count; i++)
    {
        if (chBoxListTables.GetItemChecked(i))
        {
            string str = chBoxListTables.GetItemText(i);
            MessageBox.Show(str);

            //next line invalid extension method
            chBoxListTables.Items[i].value;
        }
    }
}

This is using .Net 4.0

Any thoughts would be appreciated...thanks

Zebrass answered 14/9, 2012 at 17:58 Comment(5)
it's winform or asp.net?Sarsen
@user1669725 does chBoxListTables.CheckedItems not work?Gargoyle
It's not that I can't grab the CheckedItems, it's just that I am not able to get their text values, rather just their position ie 0,1,2Zebrass
What does chBoxListTables.Items[i].GetType() give you? Can you set a breakpoint and look at that with quickwatch?Ryley
Is 'value' actually lower-case in your code? Might wanna check that.Wester
Z
17

This ended up being quite simple. chBoxListTables.Item[i] is a string value, and an explicit convert allowed it to be loaded into a variable. The following code works:

private void btnGO_Click(object sender, EventArgs e)
{
    for (int i = 0; i < chBoxListTables.Items.Count; i++)
    {
          if (chBoxListTables.GetItemChecked(i))
        {
            string str = (string)chBoxListTables.Items[i];
            MessageBox.Show(str);
        }
    }
}
Zebrass answered 18/9, 2012 at 22:26 Comment(0)
T
4

Try to use this.

        for (int i = 0; i < chBoxListTables.Items.Count; i++)
        {
            if (chBoxListTables.Items[i].Selected)
            {
                string str = chBoxListTables.Items[i].Text;
                MessageBox.Show(str);

                var itemValue = chBoxListTables.Items[i].Value;
            }
        }

The "V" should be in CAPS in Value.

Here is another code example used in WinForm app and runs properly.

        var chBoxList= new CheckedListBox();
        chBoxList.Items.Add(new ListItem("One", "1"));
        chBoxList.Items.Add(new ListItem("Two", "2"));
        chBoxList.SetItemChecked(1, true);

        var checkedItems = chBoxList.CheckedItems;
        var chkText = ((ListItem)checkedItems[0]).Text;
        var chkValue = ((ListItem)checkedItems[0]).Value;
        MessageBox.Show(chkText);
        MessageBox.Show(chkValue);
Tallula answered 14/9, 2012 at 18:10 Comment(4)
This doesn't work. The extension method still does not exist.Zebrass
Add reference of System.Web into your project and then add "using System.Web.UI.WebControls;" into your class.Tallula
@Nitesh: Read the question.He is using winform not asp.netSarsen
@geek: System.Web.UI.WebControls is used with WinForms.Overdevelop
M
3

to get the items checked you can use CheckedItems or GetItemsChecked. I tried below code in .NET 4.5

Iterate through the CheckedItems collection. This will give you the item number in the list of checked items, not the overall list. So if the first item in the list is not checked and the second item is checked, the code below will display text like Checked Item 1 = MyListItem2.

//Determine if there are any items checked. 
if(chBoxListTables.CheckedItems.Count != 0)  
{
    //looped through all checked items and show results.
    string s = "";
    for (int x = 0; x < chBoxListTables.CheckedItems.Count; x++)
    {
        s = s + (x + 1).ToString() + " = " + chBoxListTables.CheckedItems[x].ToString()+ ", ";
    }
    MessageBox.Show(s);//show result
}

-OR-

Step through the Items collection and call the GetItemChecked method for each item. This will give you the item number in the overall list, so if the first item in the list is not checked and the second item is checked, it will display something like Item 2 = MyListItem2.

int i;  
string s;   
s = "Checked items:\n" ;  
for (i = 0; i < checkedListBox1.Items.Count; i++)  
{  
   if (checkedListBox1.GetItemChecked(i))  
   {  
      s = s + "Item " + (i+1).ToString() + " = " + checkedListBox1.Items[i].ToString() + "\n";  
   }  
}  
MessageBox.Show (s);

Hope this helps...

Muirhead answered 5/2, 2017 at 19:29 Comment(0)
C
1

//Simple example code:

foreach (var item in YourCheckedListBox.CheckedItems)
{List<string>.Add(item);}
Cilurzo answered 5/5, 2017 at 17:42 Comment(0)
L
1

You can try this:-

string values = "";
foreach(ListItem item in myCBL.Items){
if(item.Selected)
{
values += item.Value.ToString() + ",";  
}
}
values = values.TrimEnd(',');  //To eliminate comma in last.
Lifesaver answered 7/12, 2017 at 19:3 Comment(0)
P
0

Instead of this:

CheckboxList1.Items[i].value;

Try This:

CheckboxList1.Items[i].ToString();

It worked for me :)

Podium answered 12/9, 2013 at 18:25 Comment(0)
K
0

Try to use this :

 private void button1_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < chBoxListTables.Items.Count; i++)
            if (chBoxListTables.GetItemCheckState(i) == CheckState.Checked)
            {
               txtBx.text += chBoxListTables.Items[i].ToString() + " \n"; 

            }
    }
Kimber answered 5/1, 2015 at 7:58 Comment(0)
T
0

You can initialize a list of string and add those items that are selected.

Please check code, works fine for me.

List<string> modules = new List<string>();

foreach(ListItem s in chk_modules.Items)
{
    if (s.Selected)
    {
         modules.Add(s.Value);
    }
}
Triecious answered 11/3, 2018 at 12:18 Comment(1)
add <string> in the List, not sure why I cannot post with that.Triecious
M
0

This will do the trick for you:

foreach (int indexChecked in checkedListBox1.CheckedIndices)
{
    string itemtxt = checkedListBox11.Items[indexChecked];
}

It will return whatever string value is in the checkedlistbox items.

Macaulay answered 11/2, 2021 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.