简体   繁体   English

从列表框中删除选定的值

[英]Removing Selected Values from ListBox

I am getting collection of values from COL_name and adding each value to The listbox named as lstDisplay ,and the values are getting added.At the same time i need to remove the same values in COl_name from the lstAvailable ListBox,but the values are onli gettin added in lstDispaly ,its not getting deleted from the anotherList box namde as lstAvailable . 我从COL_name获取值集合并将每个值添加到名为lstDisplay的列表框中,并且值也被添加。同时,我需要从lstAvailable ListBox中删除COl_name中的相同值,但这些值仅在gettin添加到lstDispaly中 ,它不会从另一个列表框namde中删除为lstAvailable

            foreach (string drr in COl_name)
            {
                lstDispaly.Items.Add(drr);
                lstAvailable.Items.Remove(drr);
            }

Thank you.. 谢谢..

It's means, you have no drr object in lstAvailable. 这意味着,您在lstAvailable中没有drr对象。 Try to do like this: 尝试这样做:

foreach (string drr in COl_name)
            {
                lstDispaly.Items.Add(drr);
                lstAvailable.Items.RemoveAt(COL_name.IndexOf(drr));
            }

Or you can try to override Equals() method in you class 或者您可以尝试在类中重写Equals()方法

ListBox.ObjectCollection.Remove uses Equals to determine if two objects are equal. ListBox.ObjectCollection.Remove使用Equals来确定两个对象是否相等。 So if your object (what type is it?) does not override Equals menaingfully you would compare references. 因此,如果您的对象(它是什么类型?)没有令人讨厌地覆盖Equals则可以比较引用。 If both objects are not the same reference they are not considered equal then. 如果两个对象不是同一参考,则认为它们不相等。

Another reason could be that the ListBox contains objects that are not strings but you are "removing" strings. 另一个原因可能是ListBox包含的对象不是字符串,而是您正在“删除”字符串。 Then you could use RemoveAt : 然后,您可以使用RemoveAt

var obj = lstAvailable.Items.Cast<object>()
   .Select((obj, index) => new{ obj, index })
   .FirstOrDefault(x => (x.obj == null ? "" : x.obj.ToString()) == drr);
if(obj != null)
    lstAvailable.Items.RemoveAt(obj.index);

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

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