0

I want to have a rounded custom button, for this aim I created a class extended from Button class:

    public class CustomButton : Button
    {
        //Fields
        private int borderSize = 0;
        private int borderRadius = 40;
        private Color borderColor = Color.PaleVioletRed;
        [Category("Custom Controls")]

        public int BorderSize
        {
            get
            {
                return borderSize;
            }
            set
            {
                borderSize = value;
                this.Invalidate();
            }
        }
        [Category("Custom Controls")]

        public int BorderRadius 
        { 
            get 
            {
                return borderRadius; 
            }
            set
            {
                borderRadius = value;
                this.Invalidate();
            }
        }
        [Category("Custom Controls")]
        public Color BorderColor { 
            get
            {
                return borderColor;
            }
            set
            {
                borderColor = value;
                this.Invalidate();
            }
        }
        [Category("Custom Controls")]
        public Color TextColor
        {
            get
            {
                return this.ForeColor;
            }
            set
            {
                this.ForeColor = value;
            }
        }
        //Constructor
        CustomButton()
        {
            this.FlatStyle = FlatStyle.Flat;
            this.FlatAppearance.BorderSize = 0;
            this.Size = new Size(150,40);
            this.BackColor = Color.MediumSlateBlue;
            this.ForeColor = Color.White;
        }
        //Methods
        private GraphicsPath GetFigurePath(RectangleF rect,float radius)
        {
            GraphicsPath path = new GraphicsPath();
            path.StartFigure();
            path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
            path.AddArc(rect.Width - radius, rect.Y, radius, radius, 270, 90);
            path.AddArc(rect.Width - radius, rect.Height - radius, radius, radius, 0, 90);
            path.AddArc(rect.X, rect.Height-radius, radius, radius, 90, 90);
            path.CloseFigure();
            return path;
        }
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            RectangleF rectSurface = new RectangleF(0, 0, this.Width, this.Height);
            RectangleF rectBorder = new RectangleF(1, 1, this.Width - 0.8F, this.Height - 1);

            if(borderRadius > 2) //Rounded Button
            {
                using (GraphicsPath pathSurface = GetFigurePath(rectSurface, borderRadius))
                using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius-1F))
                using (Pen penSurface = new Pen(this.Parent.BackColor, 2))
                using (Pen penBorder = new Pen(borderColor, borderSize))
                {
                    penBorder.Alignment = PenAlignment.Inset;
                    //button Surface
                    this.Region = new Region(pathSurface);

                    //Draw Surface Border for HD result
                    pevent.Graphics.DrawPath(penBorder, pathBorder);

                    //Button border
                    if (borderSize >= 1)
                        //Draw Control border
                        pevent.Graphics.DrawPath(penBorder, pathBorder);
                }
            }
            else //Normal Button
            {
                //Button surface
                this.Region = new Region(rectSurface);
                //Button border
                if(borderSize >= 1)
                {
                    using (Pen penBorder = new Pen(borderColor, borderSize))
                    {
                        penBorder.Alignment = PenAlignment.Inset;
                        pevent.Graphics.DrawRectangle(penBorder, 0, 0, this.Width - 1, this.Height - 1);
                    }
                }
            }
        }
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);base.OnHandleCreated(e);
            this.Parent.BackColorChanged += new EventHandler(Container_BackColorChanged);
        }

        private void Container_BackColorChanged(object sender, EventArgs e)
        {
            if (this.DesignMode)
                this.Invalidate();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);

        }
    }
}

But when I use my custom button, I get this warning:

Field 'frmMainPanel.customButton1' is never assigned to, and will always have its default value null

And when running app, get:Runtime error in this error I get 'Object reference not set to an instance of an object.' but can't resolve this problem

6
  • 2
    Notice how line 32 has this.button1 = new ....(); but you don't have anything similar for this.customButton1? Commented Nov 2, 2022 at 7:18
  • A translation of "Field 'frmMainPanel.customButton1' is never assigned to, and will always have its default value null" is "At no point in the code of frmMainPanel do you have something that looks like customButton1 = new CustomButton();" Commented Nov 2, 2022 at 7:22
  • I can't reproduce the "this.button1 =" behaviour, but I do see that no initialization code is generated for the custom control's field variable. It seems like it might be a bug in Visual Studio. Commented Nov 2, 2022 at 7:39
  • In this video, he write this code and run it without problem. youtube.com/… Commented Nov 2, 2022 at 8:09
  • Again: it seems like it might be a bug in Visual Studio. Are you using the exact same version and build number of Visual Studio? Commented Nov 2, 2022 at 8:25

1 Answer 1

1

The default constructor CustomButton is private. Change it to public. public CustomButton()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.