c# – Unable to draw proper gradient buttons on Windows Form


I have the following simple code that should draw a gradient button, added as gradbtn.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace CustomControls.GradientControls
{
    public class Gradbtns : Button
     {
        protected override void OnPaint(PaintEventArgs pevent)
        { 
           
            base.OnPaint(pevent);

            pevent.Graphics.FillRectangle(new LinearGradientBrush(
              new PointF(0, this.Height / 2), new PointF(this.Width, this.Height / 2),
              Color.AliceBlue, Color.BurlyWood), this.ClientRectangle);
        }
    }
}

The problem drawing with the above code is that it does not enable the “Button” click action on the Form rather it appears jus as if its drawn on the form.
But using the “OnPaintBackground” event does enable the button but the Colors aren’t seen, See Below image

enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *