官网

http://www.hzhcontrols.com

前提

入行已经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自定义控件-单选框的更多相关文章

  1. (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  2. (三十)c#Winform自定义控件-文本框(三)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (三十一)c#Winform自定义控件-文本框(四)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  4. (十八)c#Winform自定义控件-提示框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  5. (八十二)c#Winform自定义控件-穿梭框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  6. (二十八)c#Winform自定义控件-文本框(一)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. (二十九)c#Winform自定义控件-文本框(二)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. c#Winform自定义控件-目录

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. winform 自定义控件(高手)

    高手推荐:https://www.cnblogs.com/bfyx/p/11364884.html   c#Winform自定义控件-目录   前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件 ...

随机推荐

  1. Java学习笔记之---API的应用

    Java学习笔记之---API的应用 (一)Object类 java.lang.Object 类 Object 是类层次结构的根类.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个 ...

  2. python 多个装饰器的调用顺序

    python 多个装饰器的调用顺序 一般情况下,在函数中可以使用一个装饰器,但是有时也会有两个或两个以上的装饰器.多个装饰器装饰的顺序是从里到外(就近原则),而调用的顺序是从外到里(就远原则). 原代 ...

  3. 简单学习之json格式说明

    json格式说明: 1.对象是一个无序的“‘名称/值’对”集合. (1)一个对象以“{”(左括号)开始,“}”(右括号)结束. (2)每个“名称”后跟一个“:”(冒号): (3)“‘名称/值’ 对”之 ...

  4. android_activity_研究(一)

    android中活动的概念(activity)是一个很重要的东东.这里有很多东东值得好好研究.最好的研究来源当然是官网啦,所以本人这里写一点对官网文章的研究心得. 一.活动(activity)的概念 ...

  5. [WPF自定义控件库]了解WPF的布局过程,并利用Measure为Expander添加动画

    1. 前言 这篇文章介绍WPF UI元素的两步布局过程,并且通过Resizer控件介绍只使用Measure可以实现些什么内容. 我不建议初学者做太多动画的工作,但合适的动画可以引导用户视线,提升用户体 ...

  6. java将复数字符串虚部实部分离,并实现加减运算

    java字符串构造复数 将字符串分解为复数的实部和虚部 定义一个复数类,数据成员有实部和虚部,根据传参不同构造方法重载,并定义复数的加减方法,以及toString方法.有难度的便是用字符串构造复数了, ...

  7. STM32F0_HAL库驱动描述——基于F1的USART串口IT中断实现解析

    从原子F103 HAL库基础串口例程来看HAL程序结构: 从main函数开始,首先是HAL库两个函数的初始化: HAL_Init(): Stm32_Clock_Init(RCC_PLL_MUL9); ...

  8. MyBatis从入门到精通:第一章数据库创建文件

    /*创建数据库mybatis,并指定编码方式为utf8,字符比较规则为utf8_general_ci*/ CREATE DATABASE mybatis DEFAULT CHARACTER SET u ...

  9. SSRS报表-级联筛选参数刷新后不能默认全选 -问题解决方案

    好久没有写博客了,最近更新完善修复了SSRS报表的一些问题,和大家分享. 问题描述: 报表中,区域->专区->省份->地级市 此四个筛选参数是联动的,在DataSet中前一父级参数作 ...

  10. Excel催化剂开源第23波-VSTO开发辅助录入功能关键技术

    Excel催化剂开源第23波-VSTO开发辅助录入功能关键技术 Excel催化剂   2019.01.12 14:10* 字数 2948 阅读 41评论 0喜欢 0 编辑文章 在Excel催化剂的几大 ...