Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#
Article

A clickable button

Rate me:
Please Sign up or sign in to vote.
3.74/5 (17 votes)
13 Jun 20021 min read 144.2K   271   17   36
An immensely handy clickable button.

Welcome to ClickableButton!

As useful and as popular as Chris Maunder's "An unclickable button" article and class is, I thought that a clickable button might also be useful. While my ClickableButton class is implemented specifically for .NET, the very same techniques can be used in nearly every object-oriented windowing library.

Sample

Here's a sample of using the ClickableButton class. Notice that we've handled the Click event to show a message box of appreciation:

Usage

To use ClickableButton, simple add it to your form, either via the WinForms designer or manually, setting properties as appropriate:

C#
class Form1 : Form {
  ClickableButton button1;
  ...
  void InitializeComponent() {
    this.button1 = new ClickableButton();
    ...
    this.button1.Location = new System.Drawing.Point(24, 24);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(200, 120);
    this.button1.TabIndex = 0;
    this.button1.Text = "Click Me!";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    ...
    this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1});
    ...
  }
}

Behavior

Notice from the sample application that the ClickableButton will show itself as a shadowed square with a text label. When the user uses the mouse to click on the ClickableButton, it will take on a depressed look 'n' feel and fire the Click event, which a hosting program can handle. For example, the sample handles the Click event like so:

C#
void button1_Click(object sender, EventArgs e) {
  MessageBox.Show("Ohhh... That's nice.", "ClickableButton Clicked");
}

Implementation

My ClickableButton class derives from the base class System.Windows.Forms.Button class and adds absolutely nothing:

C#
public class ClickableButton : System.Windows.Forms.Button
{
}

Issues

While this button is fairly functional, it's not nearly as fun as ChrisM's unclickable button. Potential ways to make it more fun include:

  • Calling the user nasty names.
  • Formatting "c:".
  • Switching the user interface language to Spanish.
  • Spawning a virus.
  • Overheating the CPU.
  • Resetting the user's MineSweeper high scores.

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
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
KostyaGavinsky29-Jul-10 6:49
KostyaGavinsky29-Jul-10 6:49 
GeneralWPF Pin
Adrian Schröder14-Sep-09 0:24
Adrian Schröder14-Sep-09 0:24 
GeneralMy Search Is Complete! Pin
Felix De Herrera15-Jul-09 13:50
Felix De Herrera15-Jul-09 13:50 
GeneralPlease ... Pin
mbaocha6-May-09 12:42
mbaocha6-May-09 12:42 
GeneralPlease anybody help me for how to sending Http request and how to get response of http for c#.Net Pin
dudhat8-Feb-08 5:48
dudhat8-Feb-08 5:48 
GeneralLOL Pin
Emil - Gabriel7-May-07 21:41
Emil - Gabriel7-May-07 21:41 
GeneralNow your in trouble Pin
Giles21-Oct-02 12:46
Giles21-Oct-02 12:46 
Generalhahahahahahahahahha Pin
Swinefeaster6-Jul-02 23:03
Swinefeaster6-Jul-02 23:03 
GeneralSome industry perspective Pin
Mike Sax25-Jun-02 7:23
Mike Sax25-Jun-02 7:23 
GeneralRe: Some industry perspective Pin
David Cunningham25-Jun-02 17:45
cofounderDavid Cunningham25-Jun-02 17:45 
GeneralRe: Some industry perspective Pin
Nish Nishant25-Jun-02 18:09
sitebuilderNish Nishant25-Jun-02 18:09 
GeneralRe: Some industry perspective Pin
Brian Delahunty5-Oct-02 2:26
Brian Delahunty5-Oct-02 2:26 
GeneralException raised when started Pin
Patko17-Jun-02 22:39
Patko17-Jun-02 22:39 
GeneralRe: Exception raised when started Pin
Rob van der Veer8-Jun-04 2:25
Rob van der Veer8-Jun-04 2:25 
GeneralImprovement Suggestion Pin
Ed Gadziemski15-Jun-02 3:25
professionalEd Gadziemski15-Jun-02 3:25 
QuestionCan you please post code for this is VB. I have need for in project Pin
14-Jun-02 22:22
suss14-Jun-02 22:22 
GeneralCool! Pin
Mazdak14-Jun-02 19:05
Mazdak14-Jun-02 19:05 
GeneralNice. But can Pin
Chris Losinger14-Jun-02 9:29
professionalChris Losinger14-Jun-02 9:29 
GeneralWOW!!!! Pin
CWizard14-Jun-02 9:14
CWizard14-Jun-02 9:14 
What is next
A editable editbox !
A listing listbox.

Laugh | :laugh:
GeneralRe: WOW!!!! Pin
NebuGranny29-Mar-04 9:55
NebuGranny29-Mar-04 9:55 
QuestionI can't get it to work? Pin
Roger Allen14-Jun-02 6:37
Roger Allen14-Jun-02 6:37 
GeneralFun Pin
Dieder14-Jun-02 4:31
Dieder14-Jun-02 4:31 
GeneralRe: Fun Pin
Marc Richarme14-Jun-02 9:10
Marc Richarme14-Jun-02 9:10 
GeneralAnother fun idea (article suggestion) Pin
Black Cat14-Jun-02 4:02
Black Cat14-Jun-02 4:02 
GeneralYou're soooooooo evil Pin
James T. Johnson13-Jun-02 21:48
James T. Johnson13-Jun-02 21:48 

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.