Triggering event when ListBox.Item's size changes
I want开发者_C百科 to update/call some function, when my ListBox.Items.Count
changes. On addition to ListBox
or on removal of the items, how to trigger a function?
There are no events for this action. You will either need to do one of the following:
- make a thread that pulls this information and alerts you (sucks)
- override a listbox and expose these make the events (ok but why?)
- simply add the code you need at the places where you add and remove to the listbox (what most people do)
really i think 3 is the way to go.
You could use an ObservableCollection as the datasource for the listbox. Then you can create handlers for the CollectionChanged event.
Since I am using AsyncBindingList and which is running on Background Worker thread. And AsyncBindingList is connected to my ListBox. So when my Worker thread finishes its work, it will update AsyncBindingList in return it will update my listBox on the form.
But On addition of items on list box, I was looking for calling a method which updates other controls. so, Finally I got the solution.
Solution is when work thread finishes it's work, it will call RunWorkerCompletedEventHandler. When It calls a Method through RunWorkerCompletedEventHandler, I triggered Forms.Invoke method, which triggered a method on in the form class, which inturn updated all the controls.
But I really don't know is this is the best way to do it. If you have any suggestion. Please post as an answer.
精彩评论