(四)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
准备工作
该控件是由多个按钮组合形成的,类似多选框和单选框,需要用到前面我们说到的控件UCBtnExt ,如果你对UCBtnExt 还不了解,请移步
我们先理一下思路,我们需要显示多个按钮,支持多选和单选,具有选中效果
开始
我们先看下有哪些属性
/// <summary>
/// 选中改变事件
/// </summary>
public event EventHandler SelectedItemChanged;
private Dictionary<string, string> m_dataSource = new Dictionary<string, string>();
/// <summary>
/// 数据源
/// </summary>
public Dictionary<string, string> DataSource
{
get { return m_dataSource; }
set
{
m_dataSource = value;
Reload();
}
} private List<string> m_selectItem = new List<string>();
/// <summary>
/// 选中项
/// </summary>
public List<string> SelectItem
{
get { return m_selectItem; }
set
{
m_selectItem = value;
if (m_selectItem == null)
m_selectItem = new List<string>();
SetSelected();
}
} private bool m_isMultiple = false;
/// <summary>
/// 是否多选
/// </summary>
public bool IsMultiple
{
get { return m_isMultiple; }
set { m_isMultiple = value; }
}
当数据源改变的时候,需要加载按钮到面板上
private void Reload()
{
try
{
ControlHelper.FreezeControl(flowLayoutPanel1, true);
this.flowLayoutPanel1.Controls.Clear();
if (DataSource != null)
{
foreach (var item in DataSource)
{
UCBtnExt btn = new UCBtnExt();
btn.BackColor = System.Drawing.Color.Transparent;
btn.BtnBackColor = System.Drawing.Color.White;
btn.BtnFont = new System.Drawing.Font("微软雅黑", 10F);
btn.BtnForeColor = System.Drawing.Color.Gray;
btn.BtnText = item.Value;
btn.ConerRadius = ;
btn.Cursor = System.Windows.Forms.Cursors.Hand;
btn.FillColor = System.Drawing.Color.White;
btn.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
btn.IsRadius = true;
btn.IsShowRect = true;
btn.IsShowTips = false;
btn.Location = new System.Drawing.Point(, );
btn.Margin = new System.Windows.Forms.Padding();
btn.Name = item.Key;
btn.RectColor = System.Drawing.Color.FromArgb(, , );
btn.RectWidth = ;
btn.Size = new System.Drawing.Size(, );
btn.TabStop = false;
btn.BtnClick += btn_BtnClick;
this.flowLayoutPanel1.Controls.Add(btn);
}
}
}
finally
{
ControlHelper.FreezeControl(flowLayoutPanel1, false);
}
SetSelected();
} void btn_BtnClick(object sender, EventArgs e)
{
var btn = sender as UCBtnExt;
if (m_selectItem.Contains(btn.Name))
{
btn.RectColor = System.Drawing.Color.FromArgb(, , );
m_selectItem.Remove(btn.Name);
}
else
{
if (!m_isMultiple)
{
foreach (var item in m_selectItem)
{
var lst = this.flowLayoutPanel1.Controls.Find(item, false);
if (lst.Length == )
{
var _btn = lst[] as UCBtnExt;
_btn.RectColor = System.Drawing.Color.FromArgb(, , );
}
}
m_selectItem.Clear();
}
btn.RectColor = System.Drawing.Color.FromArgb(, , );
m_selectItem.Add(btn.Name);
}
if (SelectedItemChanged != null)
SelectedItemChanged(this, e);
}
如果设置了初始选中项,那么还需要在加载后选中
private void SetSelected()
{
if (m_selectItem != null && m_selectItem.Count > && DataSource != null && DataSource.Count > )
{
try
{
ControlHelper.FreezeControl(flowLayoutPanel1, true);
if (m_isMultiple)
{
foreach (var item in m_selectItem)
{
var lst = this.flowLayoutPanel1.Controls.Find(item, false);
if (lst.Length == )
{
var btn = lst[] as UCBtnExt;
btn.RectColor = System.Drawing.Color.FromArgb(, , );
}
}
}
else
{
UCBtnExt btn = null;
foreach (var item in m_selectItem)
{
var lst = this.flowLayoutPanel1.Controls.Find(item, false);
if (lst.Length == )
{
btn = lst[] as UCBtnExt;
break;
}
}
if (btn != null)
{
m_selectItem = new List<string>() { btn.Name };
btn.RectColor = System.Drawing.Color.FromArgb(, , );
}
}
}
finally
{
ControlHelper.FreezeControl(flowLayoutPanel1, false);
}
}
}
至此所有的逻辑已经处理完成,下面看下完整的代码吧
// 版权所有 黄正辉 交流群:568015492 QQ:623128629
// 文件名称:UCBtnsGroup.cs
// 创建日期:2019-08-15 15:58:13
// 功能描述:按钮组
// 项目地址: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
{
public partial class UCBtnsGroup : UserControl
{
/// <summary>
/// 选中改变事件
/// </summary>
public event EventHandler SelectedItemChanged;
private Dictionary<string, string> m_dataSource = new Dictionary<string, string>();
/// <summary>
/// 数据源
/// </summary>
public Dictionary<string, string> DataSource
{
get { return m_dataSource; }
set
{
m_dataSource = value;
Reload();
}
} private List<string> m_selectItem = new List<string>();
/// <summary>
/// 选中项
/// </summary>
public List<string> SelectItem
{
get { return m_selectItem; }
set
{
m_selectItem = value;
if (m_selectItem == null)
m_selectItem = new List<string>();
SetSelected();
}
} private bool m_isMultiple = false;
/// <summary>
/// 是否多选
/// </summary>
public bool IsMultiple
{
get { return m_isMultiple; }
set { m_isMultiple = value; }
}
public UCBtnsGroup()
{
InitializeComponent();
} private void Reload()
{
try
{
ControlHelper.FreezeControl(flowLayoutPanel1, true);
this.flowLayoutPanel1.Controls.Clear();
if (DataSource != null)
{
foreach (var item in DataSource)
{
UCBtnExt btn = new UCBtnExt();
btn.BackColor = System.Drawing.Color.Transparent;
btn.BtnBackColor = System.Drawing.Color.White;
btn.BtnFont = new System.Drawing.Font("微软雅黑", 10F);
btn.BtnForeColor = System.Drawing.Color.Gray;
btn.BtnText = item.Value;
btn.ConerRadius = ;
btn.Cursor = System.Windows.Forms.Cursors.Hand;
btn.FillColor = System.Drawing.Color.White;
btn.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
btn.IsRadius = true;
btn.IsShowRect = true;
btn.IsShowTips = false;
btn.Location = new System.Drawing.Point(, );
btn.Margin = new System.Windows.Forms.Padding();
btn.Name = item.Key;
btn.RectColor = System.Drawing.Color.FromArgb(, , );
btn.RectWidth = ;
btn.Size = new System.Drawing.Size(, );
btn.TabStop = false;
btn.BtnClick += btn_BtnClick;
this.flowLayoutPanel1.Controls.Add(btn);
}
}
}
finally
{
ControlHelper.FreezeControl(flowLayoutPanel1, false);
}
SetSelected();
} void btn_BtnClick(object sender, EventArgs e)
{
var btn = sender as UCBtnExt;
if (m_selectItem.Contains(btn.Name))
{
btn.RectColor = System.Drawing.Color.FromArgb(, , );
m_selectItem.Remove(btn.Name);
}
else
{
if (!m_isMultiple)
{
foreach (var item in m_selectItem)
{
var lst = this.flowLayoutPanel1.Controls.Find(item, false);
if (lst.Length == )
{
var _btn = lst[] as UCBtnExt;
_btn.RectColor = System.Drawing.Color.FromArgb(, , );
}
}
m_selectItem.Clear();
}
btn.RectColor = System.Drawing.Color.FromArgb(, , );
m_selectItem.Add(btn.Name);
}
if (SelectedItemChanged != null)
SelectedItemChanged(this, e);
} private void SetSelected()
{
if (m_selectItem != null && m_selectItem.Count > && DataSource != null && DataSource.Count > )
{
try
{
ControlHelper.FreezeControl(flowLayoutPanel1, true);
if (m_isMultiple)
{
foreach (var item in m_selectItem)
{
var lst = this.flowLayoutPanel1.Controls.Find(item, false);
if (lst.Length == )
{
var btn = lst[] as UCBtnExt;
btn.RectColor = System.Drawing.Color.FromArgb(, , );
}
}
}
else
{
UCBtnExt btn = null;
foreach (var item in m_selectItem)
{
var lst = this.flowLayoutPanel1.Controls.Find(item, false);
if (lst.Length == )
{
btn = lst[] as UCBtnExt;
break;
}
}
if (btn != null)
{
m_selectItem = new List<string>() { btn.Name };
btn.RectColor = System.Drawing.Color.FromArgb(, , );
}
}
}
finally
{
ControlHelper.FreezeControl(flowLayoutPanel1, false);
}
}
}
}
}
namespace HZH_Controls.Controls
{
partial class UCBtnsGroup
{
/// <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.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.SuspendLayout();
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.Location = new System.Drawing.Point(, );
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(, );
this.flowLayoutPanel1.TabIndex = ;
//
// UCBtnsGroup
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.flowLayoutPanel1);
this.MinimumSize = new System.Drawing.Size(, );
this.Name = "UCBtnsGroup";
this.Size = new System.Drawing.Size(, );
this.ResumeLayout(false); } #endregion private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
}
}
用处及效果
用处:可以用选择按钮组来替换单选框和复选框,具有更和谐的界面效果
效果:

最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(四)c#Winform自定义控件-选择按钮组的更多相关文章
- 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年了,一直想做一套漂亮点的自定义控件 ...
- (三十一)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年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (四十六)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年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
随机推荐
- Elasticsearch(三) 插件安装
1.head插件 命令: ./bin/plugin install mobz/elasticsearch-head
- springboot不同环境打包
1. 场景描述 springboot+maven打包,项目中经常用到不同的环境下打包不同的配置文件,比如连接的数据库.配置文件.日志文件级别等都不一样. 2. 解决方案 在pom.xml文件中定义 2 ...
- mysql重启遇到的问题
不知道是不是每次更新 MySQL 软件之后都需要执行数据库升级指令?在我进行过的几次软件升级之后,总会在 MySQL 的日志中见到 “[ERROR] Missing system table mysq ...
- C#3.0新增功能03 隐式类型本地变量
连载目录 [已更新最新开发文章,点击查看详细] 从 Visual C# 3.0 开始,在方法范围内声明的变量可以具有隐式“类型”var. 隐式类型本地变量为强类型,就像用户已经自行声明该类型,但 ...
- [PTA] 数据结构与算法题目集 6-1 单链表逆转
List Reverse(List L) { List p, q; p = L; q = L; L = NULL; while (p) { p = p->Next; q->Next = L ...
- [leetcode] 11. Container With Most Water (medium)
原题链接 以Y坐标长度作为木桶边界,以X坐标差为桶底,找出可装多少水. 思路: 前后遍历. Runtime: 5 ms, faster than 95.28% of Java class Soluti ...
- AIX7.1安装zabbix_agent3.4
1.在zabbix官网https://www.zabbix.com/download下载Zabbix pre-compiled agents 2.Zabbix pre-compiled agents安 ...
- IO-文件输出流
一.输出流的原理 Java向文件中写数据的原理 Java程序-->JVM(java虚拟机)-->OS(操作系统)-->OS调用写数据的方法-->把数据写入到文件中 tips: ...
- google、谷歌浏览截图
对于前端好用的浏览器---谷歌浏览器(没有插件)截取全屏很难受! 特备是前端,想截图下来,好好的量一下容器之前的尺寸(手动恼火) 对于程序员来说不一定需要插件,有很多大佬应该都知道, 小白记忆不好,每 ...
- 1.4.2python网站地图爬虫(每天一更)
# -*- coding: utf-8 -*- ''' Created on 2019年5月6日 @author: 薛卫卫 ''' import urllib.request import re de ...