Hexagon Button in c Sharp

User Interface With Hexagon Buttons in C Sharp

In C Sharp by Baqir AliLeave a Comment

In visual studio windows form, to design a user interface you can not find hexagon buttons in toolbox neither you can edit any existing buttons. What we can do is to create a custom class to create a hexagon button for your user interface.

Let’s do it step by step.

  • Create a visual studio project
  • Right-click on the project in solution explorer
  • Go to Add and Class
  • Name the class as HexagonButton
  • Write down the following code in Hexagon Button class
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Hexagon
{
    class HexagonButton : Button
    {
        protected override void OnPaint(PaintEventArgs pevent)
        {
            // Create an array of points.
            Point[] myArray =
                     {
                new Point(Convert.ToInt32((33.3/Convert.ToDouble(100)*Convert.ToDouble(ClientSize.Width))),  0),
                new Point(0,ClientSize.Height/2),
                new Point( Convert.ToInt32((33.3/Convert.ToDouble(100)*Convert.ToDouble(ClientSize.Width))),ClientSize.Height),
                new Point(Convert.ToInt32((66.6/Convert.ToDouble(100)*Convert.ToDouble(ClientSize.Width))), ClientSize.Height),  // Half
                new Point(ClientSize.Width, ClientSize.Height/2),
                new Point(Convert.ToInt32((66.6/Convert.ToDouble(100)*Convert.ToDouble(ClientSize.Width))),0),
                new Point(Convert.ToInt32((33.3/Convert.ToDouble(100)*Convert.ToDouble(ClientSize.Width))), 0)
             };

            // Create a GraphicsPath object and add a polygon.
            GraphicsPath myPath = new GraphicsPath();
            myPath.AddPolygon(myArray);

            this.Region = new System.Drawing.Region(myPath);
            base.OnPaint(pevent);
        }
    }
}
  • Do not forget to inherit the class from Button class
  • Rebuild the solution
  • Go to toolbox search for Hexagon Button
  • Drag and Drop it on the Form
  • You can now change the button properties for button borders, background color, flat style, font size, or image

For icons, I have used icons8 buttons and for the color picker, I have used Just Color Picker.

You can download the project zip file. download zip file

Watch Video For Detail

Support us by sharing this post

Leave a Comment