Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / C#

A .NET Wizard control

Rate me:
Please Sign up or sign in to vote.
4.86/5 (89 votes)
24 Apr 2003CPOL7 min read 659.2K   8.7K   216  
A .NET Wizard control for the VS.IDE and client apps
using System;
using System.Collections;


namespace UtilityLibrary.Collections
{
  #region Class delegates
  public delegate void CollectionClear();
  public delegate void CollectionChange( int index, object value );
  public delegate void CollectionSet( int index, object old, object value );
  #endregion


  public class CollectionWithEvents : CollectionBase
  {
    #region Class members
    /// <summary>
    /// TRUE - skip all event raising code in class.
    /// </summary>
    private bool m_bSkipEvents;
    #endregion

    #region Class events
    /// <summary>
    /// Something canged in collection
    /// </summary>
    public event EventHandler     Changed;
    /// <summary>
    /// Raised by class before real cleaning of collection
    /// </summary>
    public event CollectionClear  Clearing;
    /// <summary>
    /// Raised by class after collection clean process
    /// </summary>
    public event CollectionClear  Cleared;
    /// <summary>
    /// Raised by class before Item will be added into collection
    /// </summary>
    public event CollectionChange Inserting;
    /// <summary>
    /// Raised by class after Item add into collection
    /// </summary>
    public event CollectionChange Inserted;
    /// <summary>
    /// Raised by class before real item removing from collection.
    /// </summary>
    public event CollectionChange Removing;
    /// <summary>
    /// Raised by class after item remove from collection storage
    /// </summary>
    public event CollectionChange Removed;
    /// <summary>
    /// Raised by class before item replace in collection. 
    /// </summary>
    public event CollectionSet    Setting;
    /// <summary>
    /// Raised by class after item replace in collection. 
    /// </summary>
    public event CollectionSet    Setted;
    #endregion

    #region Class Properties
    /// <summary>
    /// GET/SET can class raise events or not
    /// </summary>
    public bool QuietMode
    {
      get
      {
        return m_bSkipEvents;
      }
      set
      {
        if( value != m_bSkipEvents )
        {
          m_bSkipEvents = value;
        }
      }
    }
    #endregion
  
    #region Class overrides
    private void RaiseChangedEvent()
    {
      if( Changed != null && m_bSkipEvents == false )
      {
        Changed( this, EventArgs.Empty );
      }
    }

    
    protected override void OnClear()
    {
      // Any attached event handlers?
      if( Clearing != null && m_bSkipEvents == false )
      {
        // Raise event to notify all contents removed
        Clearing();
      }
      
      base.OnClear();
    } 

    protected override void OnClearComplete()
    {
      // Any attached event handlers?
      if( Cleared != null && m_bSkipEvents == false )
      {
        // Raise event to notify all contents removed
        Cleared();
      }

      base.OnClearComplete();

      RaiseChangedEvent();
    } 

    
    protected override void OnInsert( int index, object value )
    {
      // Any attached event handlers?
      if( Inserting != null && m_bSkipEvents == false )
      {
        // Raise event to notify new content added
        Inserting( index, value );
      }
      
      base.OnInsert( index, value );
    }

    protected override void OnInsertComplete( int index, object value )
    {
      // Any attached event handlers?
      if( Inserted != null && m_bSkipEvents == false )
      {
        // Raise event to notify new content added
        Inserted( index, value );
      }

      base.OnInsertComplete( index, value );

      RaiseChangedEvent();
    }

    
    protected override void OnRemove( int index, object value )
    {
      // Any attached event handlers?
      if( Removing != null && m_bSkipEvents == false )
      {
        // Raise event to notify content has been removed
        Removing( index, value );
      }

      base.OnRemove( index, value );
    }

    protected override void OnRemoveComplete( int index, object value )
    {
      // Any attached event handlers?
      if( Removed != null && m_bSkipEvents == false )
      {
        // Raise event to notify content has been removed
        Removed( index, value );
      }

      base.OnRemoveComplete( index, value );

      RaiseChangedEvent();
    }
    
    
    protected override void OnSet( int index, object oldValue, object newValue )
    {
      if( Setting != null && m_bSkipEvents == false )
      {
        Setting( index, oldValue, newValue );
      }

      base.OnSet( index, oldValue, newValue );
    }

    protected override void OnSetComplete( int index, object oldValue, object newValue )
    {
      if( Setted != null && m_bSkipEvents == false )
      {
        Setted( index, oldValue, newValue );
      }

      base.OnSetComplete( index, oldValue, newValue );

      RaiseChangedEvent();
    }
    #endregion
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO ArtfulBits Inc.
Ukraine Ukraine
Name:Kucherenko Oleksandr

Born:September 20, 1979

Platforms: Win32, Linux; - well known and MS-DOS; Win16; OS/2 - old time not touched;

Hardware: IBM PC

Programming Languages: Assembler (for Intel 80386); Borland C/C++; Borland Pascal; Object Pascal; Borland C++Builder; Delphi; Perl; Java; Visual C++; Visual J++; UML; XML/XSL; C#; VB.NET; T-SQL; PL/SQL; and etc.

Development Environments: MS Visual Studio 2001-2008; MS Visual C++; Borland Delphi; Borland C++Builder; C/C++ any; Rational Rose; GDPro; Together and etc.

Libraries: STL, ATL, WTL, MFC, NuMega Driver Works, VCL; .NET 1.0, 1.1, 2.0, 3.5; and etc.

Technologies: Client/Server; COM; DirectX; DirectX Media; BDE; HTML/DHTML; ActiveX; Java Servlets; DCOM; COM+; ADO; CORBA; .NET; Windows Forms; GDI/GDI+; and etc.

Application Skills: Databases - design and maintain, support, programming; GUI Design; System Programming, Security; Business Software Development. Win/Web Services development and etc.

Comments and Discussions