Search code examples
c#winformscheckedlistbox

Can't get correct value from Item in CheckedBoxList


Given that I added an item to CheckedListBox this way:

checkedListBox1.Items.Add("ItemA");

And let's say that this is the only item in the control:

string s = checkedListBox1.GetItemText(0);

Now the value of s is "0" when I expect "ItemA". Why can't I get correct value using checkedListBox1.GetItemText(int itemIndex) method?


Solution

  • You should pass the object which is an item of CheckedListBox to GetItemText:

    MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[0]);
    

    Otherwise GetItemText returns ToString of passed object.