当前位置:网站首页>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);
边栏推荐
- 基于加权灰色关联投影的Bagging-Blending多模型融合短期电力负荷预测
- Unity Beginner 6 - Simple UI production (blood bar production) and audio addition and NPC dialogue bubbles (2d)
- C#调用explorer.exe打开指定目录
- CMake library search function does not search LD_LIBRARY_PATH
- 13-GuliMall 基础篇总结
- The method of judging the same variable without the if branch
- LeetCode_235_二叉搜索树的最近公共祖先
- Add the device library after Vivado installation
- Manage reading notes upward
- win下怎么搭建php环境的方法教程
猜你喜欢
随机推荐
刷屏了!!!
崩了,该来的终究躲不掉
Js - 内置对象
概率论的学习整理--番外1:可重复且无次序的计数公式C(n+k-1,k) 的例题 : 同时丢3个骰子,会有多少种情况?答案不是216而是56!
mapbox-gl开发教程(十四):画圆技巧
柔性机械系统分布参数建模及其控制的研究与进展
北上广线下活动丨年底最不可错过的技术聚会都齐了
Win11打不开exe应用程序怎么办?Win11无法打开exe程序解决方法
Program environment and preprocessing (detailed)
SCM engineers written questions induction summary
Verilog语法基础HDL Bits训练 07
Add the device library after Vivado installation
Get the original data API on 1688app
什么是驱动程序签名,驱动程序如何获取数字签名?
LeetCode_235_Last Common Ancestor of Binary Search Tree
int a=8,a=a++,a? int b=8,b=b+1,b?
开源出来的fuse版pfs文件系统主要就是解决缓存问题吧。nfs挂载参数带sync规避缓存问题是不是
Verilog grammar basics HDL Bits training 08
解码Redis最易被忽视的CPU和内存占用高问题
New:WebKitX ActiveX :::Crack