Click here to Skip to main content
6,596,602 members and growing! (20,732 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Beginners     Beginner

Writing your Custom Control: step by step

By Alexandr_K

Making a cute button of two semi-transparent user-changeble colors
C#, Windows, .NET 1.0, Dev
Posted:24 Mar 2002
Updated:31 Mar 2002
Views:327,398
Bookmarked:159 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
145 votes for this article.
Popularity: 9.48 Rating: 4.38 out of 5
5 votes, 4.9%
1
5 votes, 4.9%
2
7 votes, 6.8%
3
16 votes, 15.5%
4
70 votes, 68.0%
5

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 the 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 to change the base class of the cuteButton:

    Override the following line:

    public class cuteButton : System.Windows.Forms.Control

    by this one:

    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:

    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() methoid 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:

    // 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:

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 it�s properties (cuteColor1, cuteColor2, cuteTransparent1, cuteTransparent2).

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

Good Luck.

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

About the Author

Alexandr_K


Member
Years of programming...
Occupation: Web Developer
Location: Canada Canada

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 50 (Total in Forum: 50) (Refresh)FirstPrevNext
GeneralThanks Pinmemberrutstyle1:09 20 Jun '09  
AnswerWindows Forms Control Library =~ Windows Control Library, also a partial class error Pinmemberdblanchard16:00 17 Mar '09  
General3Ks Pinmemberwhatcsu14:14 3 Aug '08  
GeneralThanks PinmemberLaurent Cozic10:10 2 Aug '08  
QuestionInclude control during runtime PinmemberVagga Raghavendra21:06 10 Aug '07  
GeneralLoad custom control dynamically PinmemberVINH TRAN13:11 20 Jul '07  
GeneralNice... PinmemberESTANNY14:41 13 Jul '07  
GeneralThanks! Just what I needed Pinmemberhalciber10:36 10 Apr '07  
GeneralTransparent control Pinmemberarijit_datta2621:38 8 Mar '07  
GeneralMany thx! Pinmemberflyingchen20:21 10 Feb '07  
Generalvery good PinmemberZhi Chen16:33 1 Feb '07  
Questioncustom control on window ce Pinmemberbuithelan16:57 31 Jan '07  
QuestionTo make Scrollable Contextmenustrip Pinmemberjeyaseelanmca18:31 5 Dec '06  
GeneralHow lock to change the font and colour PinmemberNata;0:58 20 Jun '06  
GeneralDoes not work for textbox Pinmembermonal12321:59 15 May '06  
QuestionInstaller for your new control. PinmemberBertus Kruger13:10 15 Mar '06  
Generalusing UserControl for application webform Pinmemberonebitcuongdv1:32 7 Dec '05  
Generalgreat job Pinmemberbmustata10:10 19 Feb '05  
Generalcustomer controls Pinmemberabidkayani111:40 3 Jan '05  
GeneralUsing standard control on ToolBar Pinmemberjimsleon20:12 7 Jul '04  
GeneralCan I change the Back Color of a Toolbar Control PinsussQumer Mumtaz Goraya21:32 18 May '04  
Generalgreat job Pinmembervishwa_3114:04 20 Apr '04  
Generalself contained code PinmemberBoudewijnL21:40 23 Nov '03  
GeneralRe: self contained code PinmemberCharlie Williams16:48 7 Dec '03  
GeneralCustom Control in SAME project!? Pinmemberomoshima18:18 14 Jul '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Mar 2002
Editor: Chris Maunder
Copyright 2002 by Alexandr_K
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project