A
A
Alex2020-04-06 15:56:59
.NET
Alex, 2020-04-06 15:56:59

How to create a custom button in VB .NET?

I'm trying to create a custom button in VB .NET. Created this class:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class EllipseButton
    Inherits Button
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim grPath As GraphicsPath = New GraphicsPath()
        grPath.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height)
        Me.Region = New Region(grPath)
        MyBase.OnPaint(e)
    End Sub
End Class


However, when adding a button from the Toolbox, the error "Unable to add element" occurs and is removed from the toolbox.
Where is the mistake?!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-04-06
@alexjet73

Works, even embeds in the form.

#region License
// // Разработано: Коротенко Владимиром Николаевичем (Vladimir N. Korotenko)
// // email: [email protected]
// // skype:vladimir-korotenko
// // https://vkorotenko.ru
// // Создано:  06.04.2020 16:10
#endregion

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Kvn.CustomControls
{
    public class EllipseButton : Button
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            var grPath = new GraphicsPath();
            grPath.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
            Region = new Region(grPath);
            base.OnPaint(e);
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question