user3195396
user3195396

Reputation: 254

How do I create button with rounded corners/edges on Winform C#?

Well I'll be putting an image on the button, and that image has rounded corners. How do I make this happen? I've been googling and searching for hours, still no luck on it.

Upvotes: 7

Views: 54208

Answers (3)

Andrew Morton
Andrew Morton

Reputation: 25066

You can use the rectangle shape from Microsoft Visual Basic PowerPacks.

There is documentation at How to: Draw Shapes with the OvalShape and RectangleShape Controls

I checked it works in a C# Windows forms project in VS2013 Express for Windows Desktop on Windows7 x64, targeted at both x86 and x64. User xam reports that it also works with VS2017.

private void rectangleShape1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Click!");
}

When the program is run

For convenience, you can add a new Toolbox tab and add the VB PP controls to it.

Upvotes: 3

Ollie Turner
Ollie Turner

Reputation: 1

i looked everywhere for a solution to this question, and just seemed to be hard to come by. but i got rounded corners on a button control using a function thats called during the paint event.

1. must have visual studio, create a new project
2. select a new windows form as your new project
3. add 2 x buttons to the form
4. double click anywhere on the form to open up the code window
5. delete all of the code, including the Form1 sub headers, and paste in the below

Imports System.Drawing.Drawing2D


Public Class Form1

    Public Sub buttonBorderRadius(ByRef buttonObj As Object, ByVal borderRadiusINT As Integer)
        Dim p As New Drawing2D.GraphicsPath()
        p.StartFigure()
        'TOP LEFT CORNER
        p.AddArc(New Rectangle(0, 0, borderRadiusINT, borderRadiusINT), 180, 90)
        p.AddLine(40, 0, buttonObj.Width - borderRadiusINT, 0)
        'TOP RIGHT CORNER
        p.AddArc(New Rectangle(buttonObj.Width - borderRadiusINT, 0, borderRadiusINT, borderRadiusINT), -90, 90)
        p.AddLine(buttonObj.Width, 40, buttonObj.Width, buttonObj.Height - borderRadiusINT)
        'BOTTOM RIGHT CORNER
        p.AddArc(New Rectangle(buttonObj.Width - borderRadiusINT, buttonObj.Height - borderRadiusINT, borderRadiusINT, borderRadiusINT), 0, 90)
        p.AddLine(buttonObj.Width - borderRadiusINT, buttonObj.Height, borderRadiusINT, buttonObj.Height)
        'BOTTOM LEFT CORNER
        p.AddArc(New Rectangle(0, buttonObj.Height - borderRadiusINT, borderRadiusINT, borderRadiusINT), 90, 90)
        p.CloseFigure()
        buttonObj.Region = New Region(p)
    End Sub



    Private Sub Button1_Paint(sender As Object, e As PaintEventArgs) Handles Button1.Paint
        buttonBorderRadius(sender, 25)
    End Sub

    Private Sub Button2_Paint(sender As Object, e As PaintEventArgs) Handles Button2.Paint
        buttonBorderRadius(sender, 50)
    End Sub


End Class

calling the function "buttonBorderRadius(sender, 50)" means you can set different borderRadius's for individual buttons. and because it uses the object, you can apply the same function to pictureboxes, and other controls (not all though)

so you can set a 10 pixel border radius with "buttonBorderRadius(sender, 10)" and a 50 pixel radius like this "buttonBorderRadius(sender, 50)" just change the integer as the 2nd argument in the function, and the 1st argument has to be the object variable

Upvotes: 0

Ruben-J
Ruben-J

Reputation: 2693

If you want to stick to windows forms, then you should use a picturebox and make some animations on hover etc. When you click it, it will function like a button.

Or check out these articles:

http://www.codeproject.com/Articles/15730/RoundButton-Windows-Control-Ever-Decreasing-Circle http://www.codeproject.com/Articles/10303/Elliptical-Circular-Button

However you could also use WPF if you want a nicer layout, but it's somewhat different from windows forms.

Upvotes: 1

Related Questions