[C#] Hướng Dẫn Custom Radio Button Trên Winform - Lập Trình VB.NET
Có thể bạn quan tâm
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 WinformDướ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.

Ở Radio custom lại, bạn có thể lựa chọn màu sắc check và uncheck ở 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:

Thanks for watching!
DOWNLOAD SOURCE
Tags: custom radio button c#radio button c#THÔNG TIN TÁC GIẢ
Founder 1285 bài viết 16,794,013 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. "

BÀI VIẾT LIÊN QUAN
Từ khóa » Cách Sử Dụng Radiobutton Trong C#
-
[Bài 07] - RadioButton Và CheckBox Trong C# | Lập Trình Cuộc Sống
-
Checkbox - RadioButton Trong Lập Trình C# Winforms - Freetuts
-
RadioButton Trong Lập Trình C# Winform | How Kteam
-
[Lập Trình C# Winform] - Bài 9: RadioButton | HowKteam - YouTube
-
Sử Dụng CheckBox Và RadioButton Trong C# - YouTube
-
[C# Winform Cơ Bản] Sử Dụng RadioButton
-
Bài Tập C# - Bài 3 - Sử Dụng Checkbox - RadioButton Trong Lập Trình ...
-
[Selenium C#] Bài 8: Cách Xử Lý Radio Button, Checkbox, Dropdown
-
[WPF] Hướng Dẫn Sử Dụng Radiobutton Trong WPF - C# Căn Bản
-
Ngôn Ngữ ASP.NET - Điều Khiển RadioButton
-
Bài Tập C#.NET - Bài 3 - Sử Dụng Checkbox - RadioButton Trong Lập ...
-
Làm Cách Nào để Nhóm Các Nút Radio Của Windows Form?
-
RadioButton Control - The Complete WPF Tutorial
-
[Lập Trình C# Winform] - Bài 9: RadioButton | HowKteam