简体   繁体   English

无法从列表框中删除项目

[英]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]); 要通过索引删除,请使用ContactConflictListBox.Items.Remove(ContactConflictListBox.Items[iSelectedItem]); or ContactConflictListBox.Items.RemoveAt(iSelectedItem); ContactConflictListBox.Items.RemoveAt(iSelectedItem); . Be aware, that the RemoveAt method shouldn't be used in code, it's just there for infrastructural reasons. 请注意,RemoveAt方法不应该在代码中使用,它只是出于基础结构的原因。

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

ListBox.ObjectCollection.Remove takes the object you want to remove as argument. ListBox.ObjectCollection.Remove将要删除的对象作为参数。 You have to either call ListBox.ObjectCollection.RemoveAt (which is, unfortunately, documented as infrastructure-only), or pass the object to ListBox.ObjectCollection.Remove : 您必须调用ListBox.ObjectCollection.RemoveAt (不幸的是,记录为仅基础结构),或者将对象传递给ListBox.ObjectCollection.Remove

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

(or, in case the index is not relevant:) (或者,如果索引不相关:)

ContactConflictListBox.Items.Remove(ContactConflictListBox.SelectedItem);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM