C# 中怎么利用Winform自定义一个单选框
发表于:2025-12-04 作者:千家信息网编辑
千家信息网最后更新 2025年12月04日,本篇文章给大家分享的是有关C# 中怎么利用Winform自定义一个单选框,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。添加一个用户控件,
千家信息网最后更新 2025年12月04日C# 中怎么利用Winform自定义一个单选框
本篇文章给大家分享的是有关C# 中怎么利用Winform自定义一个单选框,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
添加一个用户控件,命名为:UCRadioButton
看一下有哪些属性
[Description("选中改变事件"), Category("自定义")]
public event EventHandler CheckedChangeEvent;
private Font _Font = new Font("微软雅黑", 12);
[Description("字体"), Category("自定义")]
public new Font Font
{
get { return _Font; }
set
{
_Font = value;
label1.Font = value;
}
}
private Color _ForeColor = Color.FromArgb(62, 62, 62);
[Description("字体颜色"), Category("自定义")]
public new Color ForeColor
{
get { return _ForeColor; }
set
{
label1.ForeColor = value;
_ForeColor = value;
}
}
private string _Text = "单选按钮";
[Description("文本"), Category("自定义")]
public string TextValue
{
get { return _Text; }
set
{
label1.Text = value;
_Text = value;
}
}
private bool _checked = false;
[Description("是否选中"), Category("自定义")]
public bool Checked
{
get
{
return _checked;
}
set
{
if (_checked != value)
{
_checked = value;
if (base.Enabled)
{
if (_checked)
{
panel1.BackgroundImage = Properties.Resources.radioButton1;
}
else
{
panel1.BackgroundImage = Properties.Resources.radioButton0;
}
}
else
{
if (_checked)
{
panel1.BackgroundImage = Properties.Resources.radioButton10;
}
else
{
panel1.BackgroundImage = Properties.Resources.radioButton00;
}
}
SetCheck(value);
if (CheckedChangeEvent != null)
{
CheckedChangeEvent(this, null);
}
}
}
}
private string _groupName;
[Description("分组名称"), Category("自定义")]
public string GroupName
{
get { return _groupName; }
set { _groupName = value; }
}
public new bool Enabled
{
get
{
return base.Enabled;
}
set
{
base.Enabled = value;
if (value)
{
if (_checked)
{
panel1.BackgroundImage = Properties.Resources.radioButton1;
}
else
{
panel1.BackgroundImage = Properties.Resources.radioButton0;
}
}
else
{
if (_checked)
{
panel1.BackgroundImage = Properties.Resources.radioButton10;
}
else
{
panel1.BackgroundImage = Properties.Resources.radioButton00;
}
}
}
}
当选中状态改变时需要根据分组名称来做相应的处理
private void SetCheck(bool bln)
{
if (!bln)
return;
if (this.Parent != null)
{
foreach (Control c in this.Parent.Controls)
{
if (c is UCRadioButton && c != this)
{
UCRadioButton uc = (UCRadioButton)c;
if (_groupName == uc.GroupName && uc.Checked)
{
uc.Checked = false;
return;
}
}
}
}
}
当点击时改变选中状态
private void Radio_MouseDown(object sender, MouseEventArgs e)
{
this.Checked = true;
}
加载时做一下处理,防止多选了
private void UCRadioButton_Load(object sender, EventArgs e)
{
if (this.Parent != null && this._checked)
{
foreach (Control c in this.Parent.Controls)
{
if (c is UCRadioButton && c != this)
{
UCRadioButton uc = (UCRadioButton)c;
if (_groupName == uc.GroupName && uc.Checked)
{
Checked = false;
return;
}
}
}
}
}
来看下完整的代码吧
// 版权所有 黄正辉 交流群:568015492 QQ:623128629
// 文件名称:UCRadioButton.cs
// 创建日期:2019-08-15 16:03:13
// 功能描述:RadioButton
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HZH_Controls.Controls
{
[DefaultEvent("CheckedChangeEvent")]
public partial class UCRadioButton : UserControl
{
[Description("选中改变事件"), Category("自定义")]
public event EventHandler CheckedChangeEvent;
private Font _Font = new Font("微软雅黑", 12);
[Description("字体"), Category("自定义")]
public new Font Font
{
get { return _Font; }
set
{
_Font = value;
label1.Font = value;
}
}
private Color _ForeColor = Color.FromArgb(62, 62, 62);
[Description("字体颜色"), Category("自定义")]
public new Color ForeColor
{
get { return _ForeColor; }
set
{
label1.ForeColor = value;
_ForeColor = value;
}
}
private string _Text = "单选按钮";
[Description("文本"), Category("自定义")]
public string TextValue
{
get { return _Text; }
set
{
label1.Text = value;
_Text = value;
}
}
private bool _checked = false;
[Description("是否选中"), Category("自定义")]
public bool Checked
{
get
{
return _checked;
}
set
{
if (_checked != value)
{
_checked = value;
if (base.Enabled)
{
if (_checked)
{
panel1.BackgroundImage = Properties.Resources.radioButton1;
}
else
{
panel1.BackgroundImage = Properties.Resources.radioButton0;
}
}
else
{
if (_checked)
{
panel1.BackgroundImage = Properties.Resources.radioButton10;
}
else
{
panel1.BackgroundImage = Properties.Resources.radioButton00;
}
}
SetCheck(value);
if (CheckedChangeEvent != null)
{
CheckedChangeEvent(this, null);
}
}
}
}
private string _groupName;
[Description("分组名称"), Category("自定义")]
public string GroupName
{
get { return _groupName; }
set { _groupName = value; }
}
public new bool Enabled
{
get
{
return base.Enabled;
}
set
{
base.Enabled = value;
if (value)
{
if (_checked)
{
panel1.BackgroundImage = Properties.Resources.radioButton1;
}
else
{
panel1.BackgroundImage = Properties.Resources.radioButton0;
}
}
else
{
if (_checked)
{
panel1.BackgroundImage = Properties.Resources.radioButton10;
}
else
{
panel1.BackgroundImage = Properties.Resources.radioButton00;
}
}
}
}
public UCRadioButton()
{
InitializeComponent();
}
private void SetCheck(bool bln)
{
if (!bln)
return;
if (this.Parent != null)
{
foreach (Control c in this.Parent.Controls)
{
if (c is UCRadioButton && c != this)
{
UCRadioButton uc = (UCRadioButton)c;
if (_groupName == uc.GroupName && uc.Checked)
{
uc.Checked = false;
return;
}
}
}
}
}
private void Radio_MouseDown(object sender, MouseEventArgs e)
{
this.Checked = true;
}
private void UCRadioButton_Load(object sender, EventArgs e)
{
if (this.Parent != null && this._checked)
{
foreach (Control c in this.Parent.Controls)
{
if (c is UCRadioButton && c != this)
{
UCRadioButton uc = (UCRadioButton)c;
if (_groupName == uc.GroupName && uc.Checked)
{
Checked = false;
return;
}
}
}
}
}
}
}
namespace HZH_Controls.Controls
{
partial class UCRadioButton
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Font = new System.Drawing.Font("微软雅黑", 12F);
this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(62)))), ((int)(((byte)(62)))), ((int)(((byte)(62)))));
this.label1.Location = new System.Drawing.Point(18, 0);
this.label1.Name = "label1";
this.label1.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
this.label1.Size = new System.Drawing.Size(215, 30);
this.label1.TabIndex = 3;
this.label1.Text = "单选按钮";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Radio_MouseDown);
//
// panel1
//
this.panel1.BackgroundImage = global::HZH_Controls.Properties.Resources.radioButton0;
this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(18, 30);
this.panel1.TabIndex = 2;
this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Radio_MouseDown);
//
// UCRadioButton
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.Controls.Add(this.label1);
this.Controls.Add(this.panel1);
this.Name = "UCRadioButton";
this.Size = new System.Drawing.Size(233, 30);
this.Load += new System.EventHandler(this.UCRadioButton_Load);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Radio_MouseDown);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Panel panel1;
}
}
以上就是C# 中怎么利用Winform自定义一个单选框,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
名称
字体
代码
按钮
微软
分组
设计
C#
事件
文本
更多
状态
知识
篇文章
资源
颜色
处理
实用
内容
功能
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
孤岛惊魂4服务器
烟台软件开发有限公司
违反网络安全法的处罚方式
深圳连锁软件开发
网络安全培训分类
网络安全许可证哪个部门
东软网络安全西安杜昊军
计算机网络技术全球大学排名
软件开发阶段的三个基本活动为
阿里云磁盘数据库恢复
app软件开发分层
国家工业软件开发布局
nosql 数据库 分类
服务器运维规范
数据库镜像技术的优点是
网上订火车票数据库设计
网络技术实践主题
优化服务器下载
数据库如何接入
中国大百科全书数据库第三版
户籍地址数据库
我的世界龙珠服务器怎么切换气功
移动软件开发钱
数据库和电子商务的关联
手机版服务器怎么弄模组
ping对服务器影响
南京网络软件开发诚信合作
零基础 数据库
web服务器及网络 架构
数据库工程