通常可以使用TextBox控件的TextChanged事件进行数据验证,但如果用户不输入文本则不会进行数据验证,当然最终提交数据就可能会出错。所以想到能不能通过点击按钮再对所有的TextBox控件进行数据验证。于是在百度里搜索,功夫不负有心人,终于在《c#带验证功能的自定义控件》一文中找到相关的源代码。使用方法可以参考《
.net c# winform带验证功能的TextBox,支持正则和自定义验证函数》博文。
经过多天的阅读理解代码,不懂的不断补充学习,人笨就得多磨时间,终于翻译出了一个VB.NET版本的自定义extBox控件。
首先,把原文代码贴出备用。
接口类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
namespace Tzhtec.Controls
{
/// <summary>
/// 为自定义验证事件提供参数
/// </summary>
[Description("为自定义验证事件提供参数"), Browsable(false)]
public class CustomerEventArgs : EventArgs
{
/// <summary>
/// 是否通过验证
/// </summary>
[Description("是否通过验证"), DefaultValue(true)]
public bool Validated { get; set; }
/// <summary>
/// 获取或设置被验证的值
/// </summary>
[Description("获取或设置被验证的值")]
public string Value { get; set; }
/// <summary>
/// 获取或设置错误信息
/// </summary>
[Description("获取或设置错误信息")]
public string ErrorMessage { get; set; }
}
public delegate void CustomerValidatedHandler(object sender, CustomerEventArgs e);
public interface ITzhtecControl
{
ButtonBase Button { get; set; }
string RegexExpression { get; set; }
bool AllowEmpty { get; set; }
bool RemoveSpace { get; set; }
string EmptyMessage { get; set; }
string ErrorMessage { get; set; }
void SelectAll();
event CustomerValidatedHandler CustomerValidated;
}
}
扩展按钮功能:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Collections;
using System.Reflection;
namespace Tzhtec.Controls
{
/// <summary>
/// 创建:天智海网络
/// 邮箱:service@tzhtec.com
/// 网址:http://www.tzhtec.com
/// 功能描述:BaseButton扩展方法
/// </summary>
public static class TzhtecButton
{
private static Hashtable ht = new Hashtable(); // 存储相关控件与对应的按钮关系
private static Hashtable eventHt = new Hashtable(); // 存储按钮原有事件
private static ToolTip _tooltip; // 控件的tooltip
/// <summary>
/// 将控件添加到控制列表
/// </summary>
/// <param name="baseButton"></param>
/// <param name="control"></param>
public static void AddControl(this ButtonBase baseButton,
Control control)
{
if (control as ITzhtecControl == null) return;
if (!ht.ContainsKey(control))
{
ht.Add(control, baseButton);
HookButtonClickEvent(baseButton);
control.TextChanged += new EventHandler(control_TextChanged);
}
}
/// <summary>
/// 将控件从控制列表移除
/// </summary>
/// <param name="baseButton"></param>
/// <param name="control"></param>
public static void RemoveControl(this ButtonBase baseButton,
Control control)
{
if (control as ITzhtecControl == null) return;
if (ht.ContainsKey(control))
{
ht.Remove(control);
RemoveHookEvent(baseButton);
control.TextChanged -= new EventHandler(control_TextChanged);
}
}
/// <summary>
/// 文本改变消除提示信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void control_TextChanged(object sender, EventArgs e)
{
if (_tooltip != null) _tooltip.Dispose();
}
/// <summary>
/// 捕获按钮click事件
/// </summary>
/// <param name="baseButton"></param>
private static void HookButtonClickEvent(ButtonBase baseButton)
{
if (baseButton == null) return;
if (eventHt.Contains(baseButton)) return;
PropertyInfo pi = baseButton.GetType().GetProperty
("Events", BindingFlags.Instance | BindingFlags.NonPublic);
if (pi != null)
{
//获得事件列表
EventHandlerList eventList = (EventHandlerList)pi.GetValue
(baseButton, null);
if (eventList != null && eventList is EventHandlerList)
{
//查找按钮点击事件
FieldInfo fi = (typeof(Control)).GetField("EventClick",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
if (fi != null)
{
EventHandler eh = eventList[fi.GetValue(baseButton)] as
EventHandler; //记录原有按钮事件
if (eh != null) baseButton.Click -= eh; //移除原有的按钮事件
baseButton.Click += new EventHandler(button_Click); //替换按钮事件
eventHt.Add(baseButton, eh);
}
}
}
}
/// <summary>
/// 还原按钮click事件
/// </summary>
/// <param name="baseButton"></param>
private static void RemoveHookEvent(ButtonBase baseButton)
{
if (!ht.ContainsValue(baseButton))
{
foreach (DictionaryEntry de in eventHt) //遍历hashtable得到与之相关的按钮
{
if (de.Key == baseButton)
{
EventHandler eh = de.Value as EventHandler;
baseButton.Click -= button_Click; //移除替换事件
baseButton.Click += eh; // 还原事件
break;
}
}
eventHt.Remove(baseButton);
}
}
/// <summary>
/// 替换按钮的click事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void button_Click(object sender, EventArgs e)
{
if (sender == null) return;
// 获取当前按钮相关的所有控件,并根据tabindex升序排序
var ctls = (from item in ht.Cast<DictionaryEntry>()
where item.Value == sender as ButtonBase