在一个列表框C#.NET添加/删除项活动 [英] Events for adding/removing items in a listbox c#.NET
问题描述
我有一个列表框控件具有项目
取值动态添加和手动删除(由于删除项目按钮)。当项目的数量发生了变化,我想更新用户界面的其他部分 - 。你必须选择一些文件',即一个标题,说和一个项目计数的标题。
I have a listbox control that has item
s dynamically added to and manually removed from (due to 'remove item' button). When the number of items is changed, I would like to update other parts of the user interface - namely a caption that says 'You must choose some files.' and an item count caption.
如何事件处理程序或者有效的事件处理程序被添加到火时的项数被改变 - 例如一个的ItemAdded
或 ItemRemoved
或 ItemsChanged
How can an event handler or effectively an event handler be added to fire when the number of items is changed - e.g. an ItemAdded
or ItemRemoved
or ItemsChanged
请注意:这是没有关系的用户在列表框中选择项目
Note: This is nothing to do with the user selecting items in the listbox.
非常感谢!
推荐答案
您可以尝试使用的BindingList<>
作为数据源
,然后你该列表,而不是你的列表框的作用
- 它会自动获取更新从的BindingList
。
You can try using a BindingList<>
as your DataSource
, and then you act on that list instead of your ListBox
-- it will get the updates automatically from the BindingList
.
在的BindingList
有一个的ListChanged
事件。
在的ListChanged
事件有一个 ListChangedEventArgs
,其中包括ListChangedType枚举:
The ListChanged
event has a ListChangedEventArgs
that includes a ListChangedType enumerator:
BindingList<string> list = new BindingList<string>();
list.ListChanged += new ListChangedEventHandler(list_ListChanged);
void list_ListChanged(object sender, ListChangedEventArgs e) {
switch (e.ListChangedType){
case ListChangedType.ItemAdded:
break;
case ListChangedType.ItemChanged:
break;
case ListChangedType.ItemDeleted:
break;
case ListChangedType.ItemMoved:
break;
// some more minor ones, etc.
}
}
这篇关于在一个列表框C#.NET添加/删除项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!