Click here to Skip to main content
13,260,383 members (45,025 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 21 Nov 2005


, 7 Jul 2006
Rate this:
Please Sign up or sign in to vote.
A nice and simple button in C#.

Sample Image - springbutton.jpg


This sample shows how to build a simple button with a nice graphic interface. This control is an example of using the drawings classes and the essential keywords. I decided to draw an unusual button like this picture below:

Sample Image - abutton.jpg

First step

First of all, I declare my new class:

public class SpringButton : Control

How to expose the proprieties

After that, I expose some essential proprieties like the size in pixel of the triangles and the second color of the button. All the other proprieties that I need are available in the System.Windows.Form.Control class.

//this variable say if the
//mouse is over the contol
private bool Sel = false;
private Color BackColor2= Color.Gray;

public Color BackColorEnd
    get{return BackColor2; }
        this.Invalidate();  }

int _triangle =25;
//I add a proprety
//that's the lenght of
//a triangle rectangle (45°)

public int Triangle
    get{return _triangle;}
        //if lenght change I update 
        //the control

Mouse "lighting"

Another important step is to set the control as 'selected' when the mouse is over it. So if the mouse is over the control, the back color and the border color are inverted (see the code in the OnPaint override).

//set the button as "selected" on mouse entering
//and as not selected on mouse leaving
protected override void OnMouseEnter(EventArgs e)
    Sel = true;
protected override void OnMouseLeave(EventArgs e)
    Sel = false;

The core

The main step is this override of the OnPaint procedure. In this method, I draw directly on the control. First the central rectangle, and then the two triangles in the opposite corners. Here is the code:

protected void PaintBut(PaintEventArgs e)
    //I select the rights color 
    //To paint the button...
    Color FColor = this.BackColorEnd;
    Color BColor = this.BackColor;
    if (Sel == true)
      FColor = this.BackColor;
      BColor = this.BackColorEnd;
    //and draw(see All the code...)

The delegate

I will now explain how to use the delegate. I declared this class that I use as EventArgs. When the user clicks on the control, if the click has been on a triangle and I delegate with the TriangleEventArgs that says the triangle has been clicked.

protected override void OnMouseDown(MouseEventArgs e)
    // if the user use this delegate...
    if (this.TriangleClick != null)
    //check if the user click on the left triangle
    //or in the right with some geometrics  rules...
    //(is't possible to click all triangle at the same time )
    int x= e.X;
    int y= e.Y;
       (y>=(this.ClientRectangle.Height-_triangle-x)) )
        //try with right...
        TriangleClickEventArgs te= new TriangleClickEventArgs(false);
        //if not...
            te= new TriangleClickEventArgs(true);


If you would like to see my other works, please visit my home page:


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


About the Author

Daniele Fontani
Technical Lead
Italy Italy
I'm senior developer and architect specialized on portals, intranets, and others business applications. Particularly interested in Agile developing and open source projects, I worked on some of this as project manager and developer.

My programming experience include:

Frameworks \Technlogies: .NET Framework (C# & VB), ASP.NET, Java, php
Client languages:XML, HTML, CSS, JavaScript, angular.js, jQuery
Platforms:Sharepoint,Liferay, Drupal
Databases: MSSQL, ORACLE, MYSQL, Postgres

You may also be interested in...

Comments and Discussions

QuestionNice Pin
Abdul Mannan21-Sep-17 6:46
memberAbdul Mannan21-Sep-17 6:46 
GeneralIButtonControl Pin
gabis15-Jul-06 21:06
membergabis15-Jul-06 21:06 
GeneralAh! It's cool! Pin
Mahesh Sapre10-May-06 5:07
memberMahesh Sapre10-May-06 5:07 
GeneralRe: Ah! It's cool! Pin
Sir Zeppa'Man10-May-06 6:04
memberSir Zeppa'Man10-May-06 6:04 
GeneralNice Pin
NinjaCross10-May-06 0:51
memberNinjaCross10-May-06 0:51 
GeneralRe: Nice Pin
Sir Zeppa'Man10-May-06 1:31
memberSir Zeppa'Man10-May-06 1:31 
Generaltyu Pin
pccai24-Nov-05 2:12
memberpccai24-Nov-05 2:12 
QuestionSpring Button Pin
mr chiken23-Nov-05 6:57
membermr chiken23-Nov-05 6:57 
GeneralRe: Spring Button [modified] Pin
The_Mega_ZZTer9-Jul-06 14:50
memberThe_Mega_ZZTer9-Jul-06 14:50 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 7 Jul 2006
Article Copyright 2005 by Daniele Fontani
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid