C#删除列表框中的当前项目 [英] C# removing the current item in a listbox

查看:181
本文介绍了C#删除列表框中的当前项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中包含一堆对象,然后将列表的内容添加到列表框控件中.我的问题是,如何删除列表中的当前项目,这又将删除列表框中的当前项目?

I have a List which holds a bunch of objects, the contents of the list is then added to a listbox control. My question is, how can I remove the current item in the list which will in turn remove the current item in the listbox?

推荐答案

您只需在selectedindex_changed事件处理程序中获取选定的索引,然后删除该索引处的对象并重新填充列表框即可.

you can just get the selected index in the selectedindex_changed event handler, remove the object at that index and repopulate the listbox

int index = listbox.SelectedIndex();
listThatHoldsObjects.RemoveAt(index); 
listbox.ItemsSource = listThatHoldsObjects

这篇关于C#删除列表框中的当前项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆