Click here to Skip to main content
15,880,725 members
Articles / Programming Languages / C#

Writing Your Custom Control: Step by Step

Rate me:
Please Sign up or sign in to vote.
4.68/5 (147 votes)
31 Mar 20022 min read 854.3K   21.7K   239   75
Making a cute button of two semi-transparent user-changeable colors

Sample Image - cuteButton.jpg

Introduction

This is a short and simple demonstration of .NET Framework's capability of creating custom controls.

Here, I'm going to make a custom control and then, test my control in a Windows application. I have implemented some custom properties for my control, so you can learn how it is done in C#.

Building the Control

  1. Open Visual Studio and start a new project. Your project must be based on the Windows Control Library template. Call your project ctlCuteButton and click OK.

  2. Once you have your project open, delete the UserControl from the project. Just remove it because the 'User Control' is not exactly what we need here.

  3. Now go to the 'Project' menu: Project->Add User Control... and select the Custom Control template there. 'Custom Control' is what we need in this case. You may call it cuteButton. Now click OK. A new Custom control has been added to your project.

  4. The first thing we must do here is change the base class of the cuteButton:

    Override the following line:

    C#
    public class cuteButton : System.Windows.Forms.Control

    by this one:

    C#
    public class cuteButton : System.Windows.Forms.Button

    Now your control is based on the System.Windows.Forms.Button class.

  5. Now let's create some custom properties for our control. This is done by inserting the following code inside the cuteButton class:

    C#
    private Color m_color1 = Color.LightGreen;  //first color
    private Color m_color2 = Color.DarkBlue;    // second color
    private int m_color1Transparent = 64;       // transparency degree 
                                                // (applies to the 1st color)
    private int m_color2Transparent = 64;       // transparency degree 
                                                //  (applies to the 2nd color)
    
    public Color cuteColor1
    {
        get { return m_color1; }
        set { m_color1 = value; Invalidate(); }
    }
    
    public Color cuteColor2
    {
        get { return m_color2; }
        set { m_color2 = value; Invalidate(); }
    }
    
    public int cuteTransparent1
    {
        get { return m_color1Transparent; }
        set { m_color1Transparent = value; Invalidate(); }
    }
    
    public int cuteTransparent2
    {
        get { return m_color2Transparent; }
        set { m_color2Transparent = value; Invalidate(); }
    }

    The Invalidate() method is used to refresh the design view and all controls inside (the tip from Tom Welch).

  6. And the last thing to do before compiling our control is to override the Paint event. So let's do it:

    C#
    // Calling the base class OnPaint
    base.OnPaint(pe);
    // Create two semi-transparent colors
    Color c1 = Color.FromArgb(m_color1Transparent , m_color1);
    Color c2 = Color.FromArgb(m_color2Transparent , m_color2);
    Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, 
                                                               c1, c2, 10);
    pe.Graphics.FillRectangle (b, ClientRectangle);
    b.Dispose();
  7. Now you may compile the control by pressing <Ctrl>+<Shift>+<B>.

Here is the complete contents of cuteButton.cs file (just in case…):

Complete Code

C#
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace ctlCuteButton
{
    /// <summary>
    /// Summary description for cuteButton.
    /// </summary>

    public class cuteButton : System.Windows.Forms.Button
    {
    private Color m_color1 = Color.LightGreen; // first color
    private Color m_color2 = Color.DarkBlue;   // second color
    private int m_color1Transparent = 64;      // transparency degree 
                                               // (applies to the 1st color)
    private int m_color2Transparent = 64;      // transparency degree 
                                               // (applies to the 2nd color)

    public Color cuteColor1
    {
        get { return m_color1; }
        set { m_color1 = value; Invalidate(); }
    }

    public Color cuteColor2
    {
        get { return m_color2; }
        set { m_color2 = value; Invalidate(); }
    }

    public int cuteTransparent1
    {
        get { return m_color1Transparent; }
        set { m_color1Transparent = value; Invalidate(); }
    }

    public int cuteTransparent2
    {
        get { return m_color2Transparent; }
        set { m_color2Transparent = value; Invalidate(); }
    }

    public cuteButton()
    {
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        // Calling the base class OnPaint
        base.OnPaint(pe);
        // Create two semi-transparent colors
        Color c1 = Color.FromArgb
            (m_color1Transparent , m_color1);
        Color c2 = Color.FromArgb
            (m_color2Transparent , m_color2);
        Brush b = new System.Drawing.Drawing2D.LinearGradientBrush
            (ClientRectangle, c1, c2, 10);
        pe.Graphics.FillRectangle (b, ClientRectangle);
        b.Dispose();
    }
}
}

Testing the Control

  1. Open a new instance of the VS .NET. Create a new project choosing the Windows Application template.

  2. From a new Windows Forms project, we can add the compiled custom control to the toolbox. I do this by right-clicking the toolbox, selecting Customize Toolbox, and from the .NET Framework Components tab, clicking Browse and locating the Control Library DLL # (in our case, ctlCuteButton\bin\debug\cuteButton.dll). The component cuteButton will then appear in the Toolbox.

    You can play a bit changing its properties (cuteColor1, cuteColor2, cuteTransparent1, cuteTransparent2).

That’s all so far about building and using custom controls.

Good luck.

History

  • 25th March, 2002: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: self contained code Pin
Charlie Williams7-Dec-03 15:48
Charlie Williams7-Dec-03 15:48 
QuestionCustom Control in SAME project!? Pin
User 3874614-Jul-03 17:18
User 3874614-Jul-03 17:18 
AnswerRe: Custom Control in SAME project!? Pin
Heath Stewart29-Jul-03 1:48
protectorHeath Stewart29-Jul-03 1:48 
AnswerRe: Custom Control in SAME project!? Pin
Uwe Keim19-Aug-03 23:41
sitebuilderUwe Keim19-Aug-03 23:41 
GeneralRe: Custom Control in SAME project!? Pin
Olu2Learn15-Feb-06 14:46
Olu2Learn15-Feb-06 14:46 
Generalown events handler Pin
azusakt9-Jul-03 20:51
azusakt9-Jul-03 20:51 
GeneralError Pin
Member 1150211020-Jun-03 9:11
Member 1150211020-Jun-03 9:11 
GeneralRe: Error Pin
Ray Cassick20-Jun-03 9:17
Ray Cassick20-Jun-03 9:17 
My guess here is that it is because the control is not for ASP.NET development. It does not include code to render itself on a web page.



Paul Watson wrote:
"At the end of the day it is what you produce that counts, not how many doctorates you have on the wall."
George Carlin wrote:
"Don't sweat the petty things, and don't pet the sweaty things."
Jörgen Sigvardsson wrote:
If the physicists find a universal theory describing the laws of universe, I'm sure the a**hole constant will be an integral part of that theory.


GeneralRe: Error Pin
cuongbk15-Jan-04 5:55
cuongbk15-Jan-04 5:55 
GeneralRe: Error Pin
Anonymous16-Jul-04 14:18
Anonymous16-Jul-04 14:18 
GeneralTextBox custom component Pin
Anonymous13-Jun-03 4:22
Anonymous13-Jun-03 4:22 
Generalhelp me,vertically input text Pin
jirigala3-Dec-02 19:17
jirigala3-Dec-02 19:17 
GeneralHi there! Pin
antoine@orchus-tech22-Jun-02 10:20
antoine@orchus-tech22-Jun-02 10:20 
GeneralProblem occured Pin
16-May-02 13:47
suss16-May-02 13:47 
GeneralRe: Problem occured Pin
dblanchard17-Mar-09 16:04
dblanchard17-Mar-09 16:04 
Generalerror occur Pin
Ray Guan16-Apr-02 1:47
Ray Guan16-Apr-02 1:47 
GeneralRe: error occur Pin
Alexandr_K16-Apr-02 2:36
Alexandr_K16-Apr-02 2:36 
GeneralRe: error occur Pin
Ray Guan17-Apr-02 16:52
Ray Guan17-Apr-02 16:52 
QuestionWhat about design-time refresh? Pin
Tom Welch29-Mar-02 5:31
Tom Welch29-Mar-02 5:31 
AnswerRe: What about design-time refresh? Pin
Alexandr_K29-Mar-02 8:19
Alexandr_K29-Mar-02 8:19 
AnswerRe: What about design-time refresh? Pin
Nish Nishant29-Mar-02 15:45
sitebuilderNish Nishant29-Mar-02 15:45 
GeneralThanks Pin
27-Mar-02 15:10
suss27-Mar-02 15:10 
GeneralGood :-) Pin
Nish Nishant25-Mar-02 8:43
sitebuilderNish Nishant25-Mar-02 8:43 
GeneralRe: Good :-) Pin
Naeim Semsarilar25-Mar-02 8:55
Naeim Semsarilar25-Mar-02 8:55 
GeneralRe: Good :-) Pin
Nish Nishant25-Mar-02 8:59
sitebuilderNish Nishant25-Mar-02 8:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.