gomesh munda
gomesh munda

Reputation: 850

Weird behaviour of CheckedListBox

I have 2 CheckedListBoxes. One is Databound(name clbAnnually) while the other(clbMonthly) is not.For the unbound checkedlist box(clbMonthly), i am able to get the text of any items using the line mentioned below:

    private void clbMonthly_ItemCheck(object sender, ItemCheckEventArgs e)
    {
      string itemText = clbMonthly.Items[e.Index].ToString();
    }

On the otherhand, in case of databound checklistbox(clbAnnually) i am not able to get the text of the item using the same code.Why is it so??

    private void clbAnnually_ItemCheck(object sender, ItemCheckEventArgs e)
    {
     string itemText1 = clbAnnually.Items[e.Index].ToString();
    }

Upvotes: 0

Views: 40

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205899

Just because what you have used accidentally worked in some case doesn't mean it is correct.

The correct way that works in all scenarios is to use the specifically provided method GetItemText:

string itemText = clbMonthly.GetItemText(clbMonthly.Items[e.Index]);

string itemText1 = clbAnnually.GetItemText(clbAnnually.Items[e.Index]);

Upvotes: 2

Related Questions