(六)c#Winform自定义控件-单选框
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
目录
https://www.cnblogs.com/bfyx/p/11364884.html
准备工作
准备4个图片,分别对应选中,没选中,选中禁用,没选中禁用
复选框需要支持分组,当同一面板上具有多种选择的时候,分组就显得更为重要了
开始
添加一个用户控件,命名为:UCRadioButton
看一下有哪些属性
[Description("选中改变事件"), Category("自定义")]
public event EventHandler CheckedChangeEvent; private Font _Font = new Font("微软雅黑", );
[Description("字体"), Category("自定义")]
public new Font Font
{
get { return _Font; }
set
{
_Font = value;
label1.Font = value;
}
} private Color _ForeColor = Color.FromArgb(, , );
[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("微软雅黑", );
[Description("字体"), Category("自定义")]
public new Font Font
{
get { return _Font; }
set
{
_Font = value;
label1.Font = value;
}
} private Color _ForeColor = Color.FromArgb(, , );
[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
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Padding = new System.Windows.Forms.Padding(, , , );
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
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(, );
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(, );
this.panel1.TabIndex = ;
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(, );
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 点个星星吧
(六)c#Winform自定义控件-单选框的更多相关文章
- (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (三十)c#Winform自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十一)c#Winform自定义控件-文本框(四)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (十八)c#Winform自定义控件-提示框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (八十二)c#Winform自定义控件-穿梭框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (二十八)c#Winform自定义控件-文本框(一)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (二十九)c#Winform自定义控件-文本框(二)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- c#Winform自定义控件-目录
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- winform 自定义控件(高手)
高手推荐:https://www.cnblogs.com/bfyx/p/11364884.html c#Winform自定义控件-目录 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件 ...
随机推荐
- idea-Java文件结构
从项目目录来看 ,分级为project-module-package-classname.java 其中.java文件中包含至多一个publicclass.class文件和多个普通.class文件 . ...
- vue组件间通信六种方式(完整版)
本文总结了vue组件间通信的几种方式,如props. $emit/ $on.vuex. $parent / $children. $attrs/ $listeners和provide/inject,以 ...
- ~~核心编程(四):面向对象——单继承&复写~~
进击のpython 继承 继承的出现!就是一个作用! 为了节省代码! 为什么说节省代码? 你爸爸要是王健林 你是不是就不用愁怎么获得钱了? 为啥? 是不是钱已经有了! 直接拿来用就行了! 我们想一下奥 ...
- C语言指针专题——如何理解指针
本文为原创,欢迎转发! 最近在研读C primer plus 5版中文版,老外写的,还是很经典的,推荐给读者们,有需要的朋友可以在这里购买:C primer plus 5版中文版 指针,传说中是C语言 ...
- 不调用free会内存泄露吗?
内存泄露的概念大家可以自行百度下,本文不做阐述.本文要讲的是在程序中分配了内存,但是最后没有使用free()函数来释放这块内存,会导致内存泄露吗?比如有如下代码: #include <stdio ...
- ASP.NET CORE 入门教程(附源码)
ASP.NET CORE 入门教程 第一课 基本概念 基本概念 Asp.Net Core Mvc是.NET Core平台下的一种Web应用开发框架 符合Web应用特点 .NET Core跨平台解决方案 ...
- [原创]MySQL数据库查询和LVM备份还原学习笔记记录
一.查询语句类型: 1)简单查询 2)多表查询 3)子查询 4)联合查询 1)简单查询: SELECT * FROM tb_name; SELECT field1,field2 FROM tb_nam ...
- 如何在一个项目中兼容Wepy和Taro?
背景交待 NJ 项目启动初期,团队技术栈主要是基于 Vue,技术选择上就选择了类 Vue 的 wepy.迭代几个版本后 mpvue 出来了,简单调研了下,准备基于 mpvue-simple 开发部分页 ...
- 小白开学Asp.Net Core《三》
小白开学Asp.Net Core<三> ——界面 我胡汉三再次又回来了(距离上篇时间有点长),今天抽时间将最近对框架采用的后台界面做个记录 1.先上图 (图一) (图二) 2.界面说明 后 ...
- 【MySQL】(一)MySQL 体系结构和存储引擎
1.1.定义数据库和实例 数据库:物理操作系统文件或其他形式文件类型的集合.在MySQL数据库中,数据库文件可以是frm.MYD.MYI.ibd结尾的文件. 实例:MySQL数据库由后台线程以及一个共 ...