(六)C# Winform自定义控件-单选框
proginn468312
共 38153字,需浏览 77分钟
· 2020-07-28
准备工作
准备4个图片,分别对应选中,没选中,选中禁用,没选中禁用
单选框需要支持分组,当同一面板上具有多种选择的时候,分组就显得更为重要了
开始
添加一个用户控件,命名为: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;
}
}
用处及效果
用途: 就是单选框
效果:
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧,另本站转载地址:https://dotnet9.com/5288.html。
-
作者:冰封一夏 -
出处:http://www.hzhcontrols.com/doc.html -
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 -
GitHub:https://github.com/kwwwvagaa/NetWinformControl -
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
Dotnet9网站常驻编辑。
长按关注我,
欢迎技术交流!
点击阅读原文,查看HZHControls站点更多博文。
评论
轻松学习C#:百度行驶证C++离线SDK接入详解
效果 先看最终效果SDK 拿到完整包如图,687M解压后看看内容发现有个readme.txt,那就先看看内容1:用vs2015打开sln工程,最好用vs2015 comunity版本,可微软官网下载。2:sdk的doc目录有pdf接口文档。3:工程总入口main.cpp、请参考示例实现您的功能。4:
DotNet NB
9
C#中using的使用的几种场景
using在C#中随处可见,比如引入命名空间,这是也是每个C#的cs页面必须存在的,除了引入命名空间还有几种用法,下面将介绍这几种的使用。1、用于引入命名空间 这是最常见最基本的用法, 这样可以在程序中直
dotNET全栈开发
10
C# 优雅的处理 TCP 数据
前言Tcp是一个面向连接的流数据传输协议,用人话说就是传输是一个已经建立好连接的管道,数据都在管道里像流水一样流淌到对端。那么数据必然存在几个问题,比如数据如何持续的读取,数据包的边界等。Nagle's算法Nagle 算法的核心思想是,在一个 TCP 连接上,最多只能有一个未被确认的小数据包(小于
dotNET全栈开发
10
mica-mqtt 2.2.12 发布,简化 MqttClientTemplate 构造,方便自定义
一、简介mica-mqtt 基于 java aio 实现的简单、低延迟、高性能 的 mqtt 物联网开源组件。mica-mqtt 更加易于集成到已有服务和二次开发,降低自研物联网平台开发成本。二、功能支持 MQTT v3.1、v3.1.1 以及 v5.0 协议。支持 websocket mqtt 子
JAVA架构日记
0
C#中HttpClient的演进与避坑指南
引子 在互联网时代,与网络通信相关的功能已成为不可或缺的一部分。C# 中的 HttpClient 类是一个强大的工具,用于执行 HTTP 请求和处理响应。虽然HttpClient 类简单易用,但在实际应用中,仍然存在一些需要注意...
llovebo
0
详解数据资产入表六步法
土地财政趋向饱和,数据成财政新引擎在全球数字化转型的大趋势下,数据交易市场有望推动数据要素价值的开放共享和流通。据信通院测算,2021年全球47个重要经济体的数字经济增加值规模高达 38.1 万亿美元,我国2021年...
肉眼品世界
0
并行化优化KD树算法:使用C#实现高效的最近邻搜索
本文信息 中文名:《并行化优化KD树算法:使用C#实现高效的最近邻搜索》 英文名:"Parallelized Optimization of KD-Tree Algorithm: Implementing Efficient Nearest Neighbor Search in C#" 摘要 本文介绍了如何使...
程序设计实验室
0
从成本到利润,数据资产入表六步法
土地财政趋向饱和,数据成财政新引擎 在全球数字化转型的大趋势下,数据交易市场有望推动数据要素价值的开放共享和流通。据信通院测算,2021年全球47个重要经济体的数字经济增加值规模高达 38.1 万亿美元,我国2021...
数据工匠俱乐部
0