NEWS

[C#] Hướng dẫn custom Radio Button trên Winform

[C#] Hướng dẫn custom Radio Button trên Winform
Đăng bởi: Thảo Meo - Lượt xem: 5840 09:43:28, 22/06/2021EBOOK

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách custom Radio Button giống Material Design trên lập trình C#, Winform.

[C#] Custom Radio Button Winform

Dưới đây, là giao diện demo ứng dụng:

Bên trái là Radio Button chưa custom

và Bên phải là Radio Button đã được mình custom lại.

custom_radio_button

 

Ở Radio custom lại, bạn có thể lựa chọn màu sắc checkuncheck ở thuộc tính property của nó.

Các bạn tạo cho mình một class VBRadioButton.cs code như bên dưới:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

namespace customRadio
{
    class VBRadioButton : RadioButton
    {
        //Fields
        private Color checkedColor = Color.MediumSlateBlue;
        private Color unCheckedColor = Color.Gray;

        //Properties
        public Color CheckedColor
        {
            get
            {
                return checkedColor;
            }

            set
            {
                checkedColor = value;
                this.Invalidate();
            }
        }

        public Color UnCheckedColor
        {
            get
            {
                return unCheckedColor;
            }

            set
            {
                unCheckedColor = value;
                this.Invalidate();
            }
        }

        //Constructor
        public VBRadioButton()
        {
            this.MinimumSize = new Size(0, 21);
            this.Cursor = Cursors.Hand;
        }

        //Overridden methods
        protected override void OnPaint(PaintEventArgs pevent)
        {
            //Fields
            Graphics graphics = pevent.Graphics;
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            float rbBorderSize = 18F;
            float rbCheckSize = 12F;
            RectangleF rectRbBorder = new RectangleF()
            {
                X = 0.5F,
                Y = (this.Height - rbBorderSize) / 2, //Center
                Width = rbBorderSize,
                Height = rbBorderSize
            };
            RectangleF rectRbCheck = new RectangleF()
            {
                X = rectRbBorder.X + ((rectRbBorder.Width - rbCheckSize) / 2), //Center
                Y = (this.Height - rbCheckSize) / 2, //Center
                Width = rbCheckSize,
                Height = rbCheckSize
            };

            //Drawing
            using (Pen penBorder = new Pen(checkedColor, 1.6F))
            using (SolidBrush brushRbCheck = new SolidBrush(checkedColor))
            using (SolidBrush brushText = new SolidBrush(this.ForeColor))
            {
                //Draw surface
                graphics.Clear(this.BackColor);
                //Draw Radio Button
                if (this.Checked)
                {
                    graphics.DrawEllipse(penBorder, rectRbBorder);//Circle border
                    graphics.FillEllipse(brushRbCheck, rectRbCheck); //Circle Radio Check
                }
                else
                {
                    penBorder.Color = unCheckedColor;
                    graphics.DrawEllipse(penBorder, rectRbBorder); //Circle border
                }
                //Draw text
                graphics.DrawString(this.Text, this.Font, brushText,
                    rbBorderSize + 8, (this.Height - TextRenderer.MeasureText(this.Text, this.Font).Height) / 2);//Y=Center
            }
        }
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this.Width = TextRenderer.MeasureText(this.Text, this.Font).Width + 30;
        }
    }
}

Sau đó, các bạn bấm build project và vào winform main ở thanh Toolbox sẽ có control Radio Custom xuất hiện.

Các bạn kéo ra và chọn màu sắc check như hình bên dưới:

custom_radio_button_csharp

Thanks for watching!

DOWNLOAD SOURCE

Tags: custom radio button c#radio button c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn custom Radio Button trên Winform
Đăng bởi: Thảo Meo - Lượt xem: 5840 09:43:28, 22/06/2021EBOOK