How to: Create a Non-Rectangular Button

This example demonstrates how to create a button that is a different shape from the standard rectangular button. The code adds a button in the shape of a circle to the form and creates an event handler that displays a message when the circle is clicked.

Example

public Form2()
{
    // 
    // Required for Windows Form Designer support. 
    //
    InitializeComponent();
    // Initialize the user-defined button, 
    // including defining handler for Click message, 
    // location and size.
    myButtonObject myButton = new myButtonObject();
    EventHandler myHandler = new EventHandler(myButton_Click);
    myButton.Click += myHandler;
    myButton.Location = new System.Drawing.Point(20, 20);
    myButton.Size = new System.Drawing.Size(101, 101);
    this.Controls.Add(myButton);
}
public class myButtonObject : UserControl
{
    // Draw the new button. 
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
        Pen myPen = new Pen(Color.Black);
        // Draw the button in the form of a circle
        graphics.DrawEllipse(myPen, 0, 0, 100, 100);
        myPen.Dispose();
    }
}
// Handler for the click message. 
void myButton_Click(Object sender, System.EventArgs e)
{
    MessageBox.Show("Click");
}

Compiling the Code

This example requires a Windows Forms Application project that contains a form named Form2.

See Also

Concepts

Designing a User Interface in Visual C#

Other Resources

Button Controls

Visual C# Guided Tour