65.9K
CodeProject is changing. Read more.
Home

A clickable button

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.74/5 (17 votes)

Jun 14, 2002

1 min read

viewsIcon

147323

downloadIcon

273

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:

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:

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:

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.