При добавлении кнопки на форму, возникает ошибка
В проект добавил cs файл, туда вписал кнопку. 0 ошибок. Но при добавлении кнопки на форму, возникает данная ошибка.
Как ее исправить?
Ниже код файла
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyButton
{
public class MyButton : Button
{
private int borderSize = 0;
private int borderRadius = 50;
private Color borderColor = Color.DodgerBlue;
public int BorderSize
{
get => borderSize;
set { borderSize = value; Invalidate(); }
}
public int BorderRadius
{
get => borderRadius;
set { borderRadius = (value <= Height) ? value : Height; Invalidate(); }
}
public Color BorderColor {
get => BackColor; set { BackColor = value; }
}
public Color TextColor {
get => ForeColor; set { ForeColor = value;}
}
public MyButton(EventHandler Vutton_Resize)
{
Size = new Size(100, 200);
FlatAppearance.BorderSize = 0;
FlatStyle = FlatStyle.Flat;
BackColor = Color.Blue;
ForeColor = Color.White;
Resize += new EventHandler(Vutton_Resize);
}
private void Button_Resize(object sender, EventArgs e)
{
if (borderRadius > Height) borderRadius = Height;
}
private GraphicsPath GetFigurePath(RectangleF rectangle, float radius)
{
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.StartFigure();
graphicsPath.AddArc(rectangle.X, rectangle.Y, radius, radius, 180, 90);
graphicsPath.AddArc(rectangle.Width - radius, rectangle.Y, radius, radius, 270, 90);
graphicsPath.AddArc(rectangle.Width - radius, rectangle.Height - radius, radius, radius, 0, 90);
graphicsPath.AddArc(rectangle.X, rectangle.Height - radius, radius, radius, 90, 90);
graphicsPath.CloseFigure();
return graphicsPath;
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
RectangleF rectangleSurface = new RectangleF(0, 0, Width, Height);
RectangleF rectangleBorder = new RectangleF(1, 1, Width - 0.5F, Height - 1);
if (borderRadius > 0)
{
using (GraphicsPath graphicsPathSurface = GetFigurePath(rectangleSurface, borderRadius))
using (GraphicsPath graphicsPathBorder = GetFigurePath(rectangleBorder, borderRadius - 1F))
using (Pen penSurface = new Pen(Parent.BackColor, 2))
using (Pen penBorder = new Pen(borderColor, borderSize))
{
penBorder.Alignment = PenAlignment.Inset;
Region = new Region(graphicsPathSurface);
pevent.Graphics.DrawPath(penBorder, graphicsPathSurface);
if (borderSize >= 1)
pevent.Graphics.DrawPath(penBorder, graphicsPathBorder);
}
}
else
{
Region = new Region(rectangleSurface);
if (borderSize >= 1)
using (Pen penBorder = new Pen (borderColor, borderSize))
{
penBorder.Alignment = PenAlignment.Inset;
pevent.Graphics.DrawRectangle(penBorder, 0, 0, Width - 1, Height - 1);
}
}
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
Parent.BackColorChanged += new EventHandler(Container_BackColorChanged);
}
private void Container_BackColorChanged(object sender, EventArgs e)
{
if(DesignMode) Invalidate();
}
}
}