Windows Develop Bookmark and Share   
 index > Windows Forms Designer > New Button Design VB Express
 

New Button Design VB Express

Hi, I am trying to catch up with VB using Visual Studio Express editiion and for a form I need to design a new button. The button I need is round in shape not rectangular. Is there a quick way to create this? Other properties of the button should remain, event handling etc.

AlanCam  Saturday, February 03, 2007 4:40 PM
Hi Alan,

The Region property isn't visible in the designer. It is something that must be modified programmatically.

Ok, try this. Create a new C# Windows Forms solution. On the form, drop a button, and resize the button so that it is 50x50 pixels. Double-click the form to get the Form1_Load event. In your Form1.cs file, paste the following code over the existing code:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            Point[] pts = new Point[5];
            pts[0] = new Point(0, 25);
            pts[1] = new Point(25, 0);
            pts[2] = new Point(50, 25);
            pts[3] = new Point(25, 50);
            pts[4] = new Point(0, 25);
 
            GraphicsPath path = new GraphicsPath(pts, new byte[] { (byte)PathPointType.Line, 
                                                                   (byte)PathPointType.Line, 
                                                                   (byte)PathPointType.Line, 
                                                                   (byte)PathPointType.Line, 
                                                                   (byte)PathPointType.Line });
 
            button1.Region = new Region(path);
        }
    }


You'll see that the above code modifies the region of the button by cutting off the corners. All I've done is supply it an array of points to define the shape of the button. You'll also notice that you can only click the button inside the newly chopped region.

You can achieve cool effects with more advanced sets of points (like making glassy elliptical buttons).

In reality, what you'd probably want to do if you're creating a pure owner-drawn button, is create a control that inherits from UserControl (which will be a square shape to start out with) and then use the Region property to reshape the control to suit your needs.

Then you'd handle the painting of the various button states yourself (disabled, hot tracking, clicked, etc), since the built-in button renderer is not that flexible (which you can see in the example above - all the region property did was chop off the corners, it didn't cause the renderer to re-draw the borders using that region).

Take a look on www.codeproject.com for lots of examples of owner-drawn buttons made this way.
Patrick Sears  Sunday, February 04, 2007 11:15 PM
You can set the Region property of the button. You'll need to calculate your own points defining your circular button, add them to a Region object, and then set yourButton.Region = yourCircularRegion.

This will make the button appear circular, and will also make it so that the mouse only interacts with the button in that region.
Patrick Sears  Sunday, February 04, 2007 2:12 AM

Hi, I am sorry I do not understand your reply. I cannot find any property called Region in the properties of a button.

I also do not understand your reference to a Region I cannot find a Region object. Please remember I am a newcomer to Visual Studio Express VB.

Thanks for the reply.

AlanCam  Sunday, February 04, 2007 10:22 AM
Hi Alan,

The Region property isn't visible in the designer. It is something that must be modified programmatically.

Ok, try this. Create a new C# Windows Forms solution. On the form, drop a button, and resize the button so that it is 50x50 pixels. Double-click the form to get the Form1_Load event. In your Form1.cs file, paste the following code over the existing code:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            Point[] pts = new Point[5];
            pts[0] = new Point(0, 25);
            pts[1] = new Point(25, 0);
            pts[2] = new Point(50, 25);
            pts[3] = new Point(25, 50);
            pts[4] = new Point(0, 25);
 
            GraphicsPath path = new GraphicsPath(pts, new byte[] { (byte)PathPointType.Line, 
                                                                   (byte)PathPointType.Line, 
                                                                   (byte)PathPointType.Line, 
                                                                   (byte)PathPointType.Line, 
                                                                   (byte)PathPointType.Line });
 
            button1.Region = new Region(path);
        }
    }


You'll see that the above code modifies the region of the button by cutting off the corners. All I've done is supply it an array of points to define the shape of the button. You'll also notice that you can only click the button inside the newly chopped region.

You can achieve cool effects with more advanced sets of points (like making glassy elliptical buttons).

In reality, what you'd probably want to do if you're creating a pure owner-drawn button, is create a control that inherits from UserControl (which will be a square shape to start out with) and then use the Region property to reshape the control to suit your needs.

Then you'd handle the painting of the various button states yourself (disabled, hot tracking, clicked, etc), since the built-in button renderer is not that flexible (which you can see in the example above - all the region property did was chop off the corners, it didn't cause the renderer to re-draw the borders using that region).

Take a look on www.codeproject.com for lots of examples of owner-drawn buttons made this way.
Patrick Sears  Sunday, February 04, 2007 11:15 PM

You can use google to search for other answers

Custom Search

More Threads

• Displaymember by valuemember
• In Visual Basic .Net 2003 - capturing key press events in a ListBox.
• ComboBox in a DesignerActionItemCollection
• Custom SaveAs Dialog
• IToolboxService and the DesignSurface class
• Need to put controls in a horizontal way in a Toolbox
• How do I use AddHandler when using IDesignerHost
• Disappearing Tab Controls
• Using Web Form Designer in a Windows Form Application
• Exception has been thrown by the target of an invocation. While using DesignerHost