开发者

cant remove an item from listbox

I am trying to remove an item from listbox but is not working. even that im 开发者_开发问答sure that there exist an item to remove. any idea about what maybe going wrong?

iSelectedItem = ContactConflictListBox.ItemIndex;


if ((iSelectedItem == -1))
{
    return;
}

ContactConflictListBox.Items.Remove(iSelectedItem);


You are getting an index, not an item. To remove by index, use ContactConflictListBox.Items.Remove(ContactConflictListBox.Items[iSelectedItem]); or ContactConflictListBox.Items.RemoveAt(iSelectedItem);. Be aware, that the RemoveAt method shouldn't be used in code, it's just there for infrastructural reasons.


if (ListBox.SelectedItem!= null)
{
    ListBox.Items.Remove(ListBox.SelectedItem); 
}


ListBox.ObjectCollection.Remove takes the object you want to remove as argument. You have to either call ListBox.ObjectCollection.RemoveAt (which is, unfortunately, documented as infrastructure-only), or pass the object to ListBox.ObjectCollection.Remove:

ContactConflictListBox.Items.Remove(ContactConflictListBox.Items[iSelectedItem]);

(or, in case the index is not relevant:)

ContactConflictListBox.Items.Remove(ContactConflictListBox.SelectedItem);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜