using System.Collections; using System.ComponentModel; namespace FarsiLibrary.Win.Helpers { /// /// Represents the method that will handle the event that has no data. /// public delegate void CollectionClear(); /// /// Represents the method that will handle the event that has item data. /// public delegate void CollectionChange(int index, object value); /// /// Extend collection base class by generating change events. /// public abstract class CollectionWithEvents : CollectionBase { // Instance fields private int _suspendCount; /// /// Occurs just before the collection contents are cleared. /// [Browsable(false)] public event CollectionClear Clearing; /// /// Occurs just after the collection contents are cleared. /// [Browsable(false)] public event CollectionClear Cleared; /// /// Occurs just before an item is added to the collection. /// [Browsable(false)] public event CollectionChange Inserting; /// /// Occurs just after an item has been added to the collection. /// [Browsable(false)] public event CollectionChange Inserted; /// /// Occurs just before an item is removed from the collection. /// [Browsable(false)] public event CollectionChange Removing; /// /// Occurs just after an item has been removed from the collection. /// [Browsable(false)] public event CollectionChange Removed; /// /// Initializes DrawTab new instance of the CollectionWithEvents class. /// public CollectionWithEvents() { // Default to not suspended _suspendCount = 0; } /// /// Do not generate change events until resumed. /// public void SuspendEvents() { _suspendCount++; } /// /// Safe to resume change events. /// public void ResumeEvents() { --_suspendCount; } /// /// Gets DrawTab value indicating if events are currently suspended. /// [Browsable(false)] public bool IsSuspended { get { return (_suspendCount > 0); } } /// /// Raises the Clearing event when not suspended. /// protected override void OnClear() { if (!IsSuspended) { // Any attached event handlers? if (Clearing != null) Clearing(); } } /// /// Raises the Cleared event when not suspended. /// protected override void OnClearComplete() { if (!IsSuspended) { // Any attached event handlers? if (Cleared != null) Cleared(); } } /// /// Raises the Inserting event when not suspended. /// /// Index of object being inserted. /// The object that is being inserted. protected override void OnInsert(int index, object value) { if (!IsSuspended) { // Any attached event handlers? if (Inserting != null) Inserting(index, value); } } /// /// Raises the Inserted event when not suspended. /// /// Index of inserted object. /// The object that has been inserted. protected override void OnInsertComplete(int index, object value) { if (!IsSuspended) { // Any attached event handlers? if (Inserted != null) Inserted(index, value); } } /// /// Raises the Removing event when not suspended. /// /// Index of object being removed. /// The object that is being removed. protected override void OnRemove(int index, object value) { if (!IsSuspended) { // Any attached event handlers? if (Removing != null) Removing(index, value); } } /// /// Raises the Removed event when not suspended. /// /// Index of removed object. /// The object that has been removed. protected override void OnRemoveComplete(int index, object value) { if (!IsSuspended) { // Any attached event handlers? if (Removed != null) Removed(index, value); } } /// /// Returns the index of the first occurrence of DrawTab value. /// /// The object to locate. /// Index of object; otherwise -1 protected int IndexOf(object value) { // Find the 0 based index of the requested entry return List.IndexOf(value); } } }