Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#

Targeting Design-Time Events of User Controls

Rate me:
Please Sign up or sign in to vote.
4.63/5 (24 votes)
22 Mar 2006CPOL6 min read 114K   1K   48  
The article will go over various design-time features including smart tags, menu items, adornments.
/* ************************************************************
 * Code Copyright 2006 by the author of 'Targeting Design-Time Events of User Controls' 
 * article that can be found at TheCodeProject.com.  
 * 
 * This code is distributed under the GPL license
 * 
 * This copyright notice is not to be removed at any time
 * 
 * ===========================================================================
 * Target
 * 
 * This control was designed for the purpose of the TheCodeProject
 * article and may not be appropriate for use in commercial applications
 * 
 * This control has a designer specified before the class called the 
 * Target_Designer.  By setting the designer of this control
 * we will allow the control to have full design time support.  The support
 * will increase productivity.
 * 
 * NOTE: Only public methods and properties can be shown on smart tag
 * methods and properties.
 * ************************************************************
 */
using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

[Serializable()]
[Designer(typeof(Target_Designer))]
public class Target:System.Windows.Forms.UserControl
{
    #region Initialization
    public Target()
    {
        //This is just a view styles that would be good idea to put
        //with every custom control.
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.BackColor = Color.Transparent;
    } 
    #endregion

    Collection<Circle> cc = new Collection<Circle>();
    Color c1=Color.Red, c2=Color.White;

    #region Properties
    [TypeConverter()]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Collection<Circle> Circles
    {
        get { return cc; }
        set { cc = value; }
    }
    public Color Color1
    {
        get{return c1;}
        set { c1 = value; this.Invalidate(); }
    }
    public Color Color2
    {
        get{return c2;}
        set { c2 = value; this.Invalidate(); }
    }
    #endregion

    #region Painting
    protected override void OnPaint(PaintEventArgs e)
    {
        Color active = c1;
        Sort(cc);
        foreach (Circle c in cc)
        {
            e.Graphics.FillEllipse(new SolidBrush(active), this.Width / 2 - c.Diameter / 2, this.Height / 2 - c.Diameter / 2, c.Diameter, c.Diameter);
            active = active == c1 ? c2 :c1 ;
        }
    } 
    #endregion

    #region Sort
    internal void Sort(Collection<Circle> c)
    {
        for (int k = 0; k < c.Count - 1; k++)
        {
            Circle tmp;
            if (c[k].Diameter < c[k + 1].Diameter)
            {
                tmp = c[k];
                c[k] = c[k + 1];
                c[k + 1] = tmp;
            }
        }
    } 
    #endregion
}
#region Class Circle
public class Circle
{
    int diameter;
    #region Initialization
    public Circle()
    {
    }
    public Circle(int Diameter)
    {
        diameter = Diameter;
    }
    #endregion
    #region Properties
    public int Diameter
    {
        get { return diameter; }
        set { diameter = value; }
    }
    #endregion
} 
#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
Software Developer University of Michigan-Flint
United States United States
I am currently a Database Administrator & Developer for the International Center at the University of Michigan. My expertise is GUI design with WinForms and WPF.

Comments and Discussions