Click here to Skip to main content
16,004,452 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 695K   8.7K   216  
A .NET Wizard control for the VS.IDE and client apps
using System;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using UtilityLibrary.Collections;

namespace UtilityLibrary.Menus
{
  [ToolboxItem(false)]
  public class MenuCommand : Component
  {
    // Enumeration of property change events
    public enum Property
    {
      Text,
      Enabled,
      ImageIndex,
      ImageList,
      Shortcut,
      Checked,
      RadioCheck,
      Break,
      Infrequent,
      Visible,
      Image, 
      RealOwner,
      ComboBox
    }

    // Declare the property change event signature
    public delegate void PropChangeHandler(MenuCommand item, Property prop);

    // Public events
    public event PropChangeHandler PropertyChanged;

    // Instance fields
    protected bool      visible;
    protected bool      _break;
    protected string    text;
    protected bool      enabled;
    protected bool      _checked;
    protected int       imageIndex;
    protected bool      infrequent;
    protected object    userData;
    protected bool      radioCheck;
    protected Shortcut  shortcut;
    protected ImageList imageList;
    protected Bitmap    image;
    protected Object    realOwner;
    protected ComboBox  comboBox;
    protected MenuCommandCollection menuItems;

    // Exposed events
    public event EventHandler Click;
    public event EventHandler Update;

    public MenuCommand()
    {
      InternalConstruct("MenuItem", null, -1, Shortcut.None, null, null, null);
    }

    public MenuCommand(string text)
    {
      InternalConstruct(text, null, -1, Shortcut.None, null, null, null);
    }

    public MenuCommand(string text, EventHandler clickHandler)
    {
      InternalConstruct(text, null, -1, Shortcut.None, clickHandler, null, null);
    }

    public MenuCommand(string text, Shortcut shortcut)
    {
      InternalConstruct(text, null, -1, shortcut, null, null, null);
    }

    public MenuCommand(string text, Shortcut shortcut, EventHandler clickHandler)
    {
      InternalConstruct(text, null, -1, shortcut, clickHandler, null, null);
    }

    public MenuCommand(string text, ImageList imageList, int imageIndex)
    {
      InternalConstruct(text, imageList, imageIndex, Shortcut.None, null, null, null);
    }

    public MenuCommand(string text, ImageList imageList, int imageIndex, Shortcut shortcut)
    {
      InternalConstruct(text, imageList, imageIndex, shortcut, null, null, null);
    }

    public MenuCommand(string text, ImageList imageList, int imageIndex, 
      Shortcut shortcut, EventHandler clickHandler)
    {
      InternalConstruct(text, imageList, imageIndex, shortcut, clickHandler, null, null);
    }

    public MenuCommand(string text, Bitmap bitmap, Shortcut shortcut, EventHandler clickHandler, Object realOwner)
    {
      InternalConstruct(text, null, -1, shortcut, clickHandler, bitmap, realOwner);
    }

    public MenuCommand(ComboBox comboBox)
    {
      InternalConstruct("", null, -1, Shortcut.None, null, null, null);
      this.comboBox = comboBox;
    }

    protected void InternalConstruct(string text, ImageList imageList, int imageIndex, 
      Shortcut shortcut, EventHandler clickHandler, Bitmap bitmap, object realOwner)
    {
      // Save parameters
      this.text       = text;
      this.imageList  = imageList;
      this.imageIndex = imageIndex;
      this.shortcut   = shortcut;
      image           = bitmap;
      this.realOwner  = realOwner;
      this.comboBox   = null;

      if( clickHandler != null )
      {
        Click += clickHandler;
      }
    
      // Define defaults for others
      enabled     = true;
      _checked    = false;
      radioCheck  = false;
      _break      = false;
      userData    = null;
      visible     = true;
      infrequent  = false;

      // Create the collection of embedded menu commands
      menuItems = new MenuCommandCollection();
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public MenuCommandCollection MenuCommands
    {
      get { return menuItems; }
      
      set 
      {
        menuItems.Clear();
        menuItems = value;
      }
    }

    public string Text
    {
      get { return text; }
      
      set 
      { 
        if (text != value)
        {
          text = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.Text);
          }
        } 
      }
    }

    public bool Enabled
    {
      get { return enabled; }

      set 
      {
        if (enabled != value)
        {
          enabled = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.Enabled);
          }
        }
      }
    }

    public int ImageIndex
    {
      get { return imageIndex; }

      set 
      { 
        if (imageIndex != value)
        {
          imageIndex = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.ImageIndex);
          }
        } 
      }
    }

    public ImageList ImageList
    {
      get { return imageList; }

      set 
      { 
        if (imageList != value)
        {
          imageList = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.ImageList);
          }
        }
      }
    }

    public Shortcut Shortcut
    {
      get { return shortcut; }

      set 
      { 
        if (shortcut != value)
        {
          shortcut = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.Shortcut);
          }
        }
      }
    }

    public bool Checked
    {
      get { return _checked; }

      set 
      { 
        if (_checked != value)
        {
          _checked = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.Checked);
          }
        }
      }
    }

    public bool RadioCheck
    {
      get { return radioCheck; }

      set 
      { 
        if (radioCheck != value)
        {
          radioCheck = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.RadioCheck);
          }
        }
      }
    }

    public bool Break
    {
      get { return _break; }
      
      set 
      { 
        if (_break != value)
        {
          _break = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.Break);
          }
        }
      }
    }

    public bool Infrequent
    {
      get { return infrequent; }
      
      set 
      { 
        if (infrequent != value)
        {
          infrequent = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.Infrequent);
          }
        }
      }
    }

    public bool Visible
    {
      get { return visible; }

      set 
      { 
        if (visible != value)
        {
          visible = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.Visible);
          }
        }
      }
    }

    public Bitmap Image
    {
      get { return image; }

      set 
      { 
        if (image != value)
        {
          image = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.Image);
          }
        }
      }
    }

    public Object RealOwner
    {
      get { return realOwner; }

      set 
      { 
        if (realOwner != value)
        {
          realOwner = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.RealOwner);
          }
        }
      }
    }

    public ComboBox ComboBox 
    {
      get { return comboBox; }

      set 
      { 
        if (comboBox != value)
        {
          comboBox = value;

          // Any attached event handlers?
          if (PropertyChanged != null)
          {
            // Raise event to notify property value has changed
            PropertyChanged(this, Property.ComboBox);
          }
        }
      }
    }

    public object UserData
    {
      get { return userData; }
      set { userData = value; }
    }

    public virtual void OnClick(EventArgs e)
    {
      if( realOwner != null && Click != null )
      {
        Click( realOwner, e );
      }
      else if( Click != null )
      {
        Click( this, e );
      }
    }

    public virtual void OnUpdate(EventArgs e)
    {
      if (Update != null)
        Update(this, e);
    }
  }
}

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