当前位置:网站首页>WinForm枚举容器中的控件,实现控件统一事件处理机制
WinForm枚举容器中的控件,实现控件统一事件处理机制
2022-07-30 12:04:00 【黄昏和星空】
我们知道,要在应用程序中使用事件,必须提供一个事件处理程序(事件处理方法),这通常用委托来实现。但当想对某个容器中的同类控件的相同事件都实现相同的处理方法时,可能通过枚举容器中的控件并指定相关委托来实现事件的处理。或许你会说,干吗要说得这么复杂,我可以在控件的属性中指定事件处理方法来实现,但当容器中的控件很多,或者在设计过程中加入了新的控件,逐个指定毕竟是很麻烦的一件事。
如下图所示,在应用中有两个文本框,我想指定两个文本框(用户名与密码)的Enter、Leave与Validating事件,当文本框获得焦点时改变其背景色,当离开时还原为系统文本颜色,并用Validating方法检查文本框内容是否为空,为空时用ErrorProvider显示错误信息。
我通过方法AddEventHandler来枚举窗体中的控件,当它是文本框时指定事件的委托,代码如下:
/** <summary>
/// 枚举容器中的控件,并增加文本框的事件处理委托
/// </summary>
/// <param name="pnl">container</param>
private void AddEventHandler(Control pnl)
{
foreach(Control ctrl in pnl.Controls)
{
if(ctrl is TextBox)
{
ctrl.Enter+=new EventHandler(this.txt_Enter);
ctrl.Leave+=new EventHandler(this.txt_Leave);
ctrl.TextChanged+=new EventHandler(this.txt_TextChanged);
ctrl.Validating+=new CancelEventHandler(this.txt_Validating);
}
if(ctrl.HasChildren)
//递归注册事件。
AddEventHandler(ctrl);
}
}
/** <summary>
/// 实现文本框的Enter事件处理方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_Enter(object sender,System.EventArgs e)
{
TextBox tb=(TextBox)sender;
tb.BackColor=Color.BlanchedAlmond;
}
/** <summary>
/// 实现文本框的Leave事件处理方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_Leave(object sender,System.EventArgs e)
{
TextBox tb=(TextBox)sender;
tb.BackColor=Color.FromKnownColor(KnownColor.Window);
}
/** <summary>
/// 实现文本框的Validating事件处理方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{
TextBox tb=(TextBox)sender;
errorProvider1.SetError(tb,"");
if(tb.Text.Length==0)
{
errorProvider1.SetError(tb,"Please input any text!");
tb.Focus();
}
}
然后在Form1的构造函数中增加AddEventHandler(this)语句:
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
AddEventHandler(this);
边栏推荐
- Underwater target detection method based on spatial feature selection
- JD.com was brutally killed by middleware on two sides. After 30 days of learning this middleware booklet, it advanced to Ali.
- 从“校园贷”到“直播带货”,追风少年罗敏一直行走在风口浪尖
- 为什么说Prometheus是足以取代Zabbix的监控神器?
- JS事件的相关特性以及原理
- OneNote如何修改已有的笔记本为默认的快速笔记?
- 句柄与指针的简单理解
- 开源出来的fuse版pfs文件系统主要就是解决缓存问题吧。nfs挂载参数带sync规避缓存问题是不是
- Matlab基础(2)——向量与多项式
- unity对象池(学习)
猜你喜欢

AlphaFold预测了几乎所有已知蛋白质!涵盖100万物种2.14亿结构,数据集开放免费用...

【32. 图中的层次(图的广度优先遍历)】

崩了,该来的终究躲不掉

unity对象池(学习)

来n遍剑指--04. 二维数组中的查找

Apifox generates interface documentation tutorial and operation steps

Add the device library after Vivado installation

Zhou Hongyi: Microsoft copied the 360 security model and became the largest security company in the United States

LeetCode_236_Last Common Ancestor of a Binary Tree

Interviewer: Redis bloom filter and the cuckoo in the filter, how much do you know?
随机推荐
MySQL查询性能优化
Transfer Learning Technology Training
基于多目标两阶段随机规划方法的电热联合系统调度
Reverse linked list - iterative inversion method
External Force Estimation Based on Time Delay Estimation with Perturbed Kalman Filter
【32. 图中的层次(图的广度优先遍历)】
LeetCode_235_Last Common Ancestor of Binary Search Tree
概率论的学习整理3: 概率的相关概念
DOM常用方法以及项目
重建丢失的数据
CMake library search function does not search LD_LIBRARY_PATH
The method of judging the same variable without the if branch
Program environment and preprocessing (detailed)
小心 transmittable-thread-local 的这个坑
云主机上的MongoDB被威胁,开启AUTH认证
什么是驱动程序签名,驱动程序如何获取数字签名?
Based on MySQL database, Redis cache, MQ message middleware, ES high availability scheme of search engine parsing
数据湖(十八):Flink与Iceberg整合SQL API操作
Difference between C# enumeration type and xaml
数字量输入输出模块DAM-5088