[C#] Hướng Dẫn Custom Radio Button Trên Winform - Lập Trình VB.NET

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Ả

Founder Founder 1285 bài viết 16,793,670
NGUYỄN THẢO

Founder at LaptrinhVB.net

★★★★★

♥ Tình yêu thương chẳng hề hư mất bao giờ. (Cr 13,4)

=========================================================================

My skills includes .NET(C#, VB.NET), DevExpress, Java, Android, PHP,

Python, Sqlserver, Mysql, Reactjs, Dart, Flutter, API services and lot more...

Phone/Zalo/Telegram/WhatsApp: ☎️ (+84)933.913122

Zalo: https://zalo.me/0933913122

Email: [email protected]

My Github: https://github.com/nguyenthao1988

Facebook: https://fb.com/Lewandowski28031988

Youtube Channel: https://www.youtube.com/@thaomeotv      

=========================================================================

" Gửi tặng tác giả: Thảo Meo ly cà phê đầy năng lượng và cảm hứng. "

Mời Ad ly cafe

BÀI VIẾT LIÊN QUAN

Từ khóa » Cách Sử Dụng Radiobutton Trong C#