Property changed event in standard control
-
I would like to add a CountChanged event to the standard ListBox control. Is it possible to access the properties of a standard MS control in this way and if so could it be done runtime or would I need to create a custom control? I have a basic understanding of the event raising and handling protocol but I'm scratching my head on how to link the property. Any pointers gratefully received.
-
I would like to add a CountChanged event to the standard ListBox control. Is it possible to access the properties of a standard MS control in this way and if so could it be done runtime or would I need to create a custom control? I have a basic understanding of the event raising and handling protocol but I'm scratching my head on how to link the property. Any pointers gratefully received.
If you use VB2010 (also Express Version)you can extend or change standard methods – and I guess it's the same with controls – by using 'Extension Methods'. If you google for that, you'll find plenty of information and sample code. In VB the Extension Methods have to be defined in a module, not a class.
-
I would like to add a CountChanged event to the standard ListBox control. Is it possible to access the properties of a standard MS control in this way and if so could it be done runtime or would I need to create a custom control? I have a basic understanding of the event raising and handling protocol but I'm scratching my head on how to link the property. Any pointers gratefully received.
-
How to: Implement an Extender Provider[^]
Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]
Thanks for the suggestions guys but neither really fits the bill. I could possibly write extension methods for all the possible ways that the number of items in a List Box and set off an event in each one but that seems rather cumbersome and subject to possible errors. Extender Provider adds properties to controls but I don't need another property, I need to monitor an existing one. The intention is to have an event trigger every time that the count of list items changes for any reason. Such an event could be used to warn when a list of attendees becomes oversubscribed, for example. Now clearly it is possible to monitor the ListBox.Count property using a timer, for example, which probably is more than good enough for nearly all uses. I just wondered whether it was possible to raise an event directly. Guess the answer's, not.
-
Thanks for the suggestions guys but neither really fits the bill. I could possibly write extension methods for all the possible ways that the number of items in a List Box and set off an event in each one but that seems rather cumbersome and subject to possible errors. Extender Provider adds properties to controls but I don't need another property, I need to monitor an existing one. The intention is to have an event trigger every time that the count of list items changes for any reason. Such an event could be used to warn when a list of attendees becomes oversubscribed, for example. Now clearly it is possible to monitor the ListBox.Count property using a timer, for example, which probably is more than good enough for nearly all uses. I just wondered whether it was possible to raise an event directly. Guess the answer's, not.
Member 9082365 wrote:
The intention is to have an event trigger every time that the count of list items changes for any reason
Actually, this is a piss-poor idea. A ListBox control should not be the "authoritative" collection of items, such as your list of attendees. A dedicated collection should be the one that controls this and either raises its own event or throws an exception of the collection is considered full. The ListBox should be used as it was designed, as a visual representation of the backing collection, nothing more. Your idea is not quite as simple as creating your own version of ListBox with this little functionality added. The ListBox Items collection is a seperate class, called ObjectCollection I believe. You'd have to inherit from ObjectCollection, add your CountChanged event to it, the create your own ListBox implementation, changing out the Items collection with one that uses your new ObjectCollection implementation, handle the ObjectCollection.CountChanged event you made, and, finally, the ListBox would have to expose it's own CountChanged event that fires whenever the underlying ObjectCollection.CountChanged event was raised. ...or some variant thereof. But, the basic concept doesn't change from this.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Member 9082365 wrote:
The intention is to have an event trigger every time that the count of list items changes for any reason
Actually, this is a piss-poor idea. A ListBox control should not be the "authoritative" collection of items, such as your list of attendees. A dedicated collection should be the one that controls this and either raises its own event or throws an exception of the collection is considered full. The ListBox should be used as it was designed, as a visual representation of the backing collection, nothing more. Your idea is not quite as simple as creating your own version of ListBox with this little functionality added. The ListBox Items collection is a seperate class, called ObjectCollection I believe. You'd have to inherit from ObjectCollection, add your CountChanged event to it, the create your own ListBox implementation, changing out the Items collection with one that uses your new ObjectCollection implementation, handle the ObjectCollection.CountChanged event you made, and, finally, the ListBox would have to expose it's own CountChanged event that fires whenever the underlying ObjectCollection.CountChanged event was raised. ...or some variant thereof. But, the basic concept doesn't change from this.
A guide to posting questions on CodeProject[^]
Dave KreskowiakWell thanks for the lecture. It was only an off-the-top-of-my head example (I don't know what the initial enquirer has in mind) but feel free to belittle us all. We so love it. As for the actual information, it is much as I suspected. Something I'll maybe take on when I've a few spare days (or weeks!)
-
I would like to add a CountChanged event to the standard ListBox control. Is it possible to access the properties of a standard MS control in this way and if so could it be done runtime or would I need to create a custom control? I have a basic understanding of the event raising and handling protocol but I'm scratching my head on how to link the property. Any pointers gratefully received.