防止在文本框中输入特殊字符(解决方案中已经有数千个文本框)

To prevent entering special characters on textbox ( Already have thousand of textboxes in solution )(防止在文本框中输入特殊字符(解决方案中已经有数千个文本框))
本文介绍了防止在文本框中输入特殊字符(解决方案中已经有数千个文本框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用 c# ( windows forms ) 编写的高级软件.在他们中,我们有 1000 个或更多的文本框.我需要验证所有这些文本框上的用户输入,以停止输入特殊字符和任何脚本.文本框是硬编码的.

We have a advanced software written by using c# ( windows forms ). In their we have 1000 or more textboxes. I need to validate user input on all these textboxes to stop entering special characters and any scripts. Textboxes are hard coded.

例如:我可以在每次按键时使用以下代码来检查用户是否输入了允许的字符.

for eg : I can use following piece of code on every keypress to check whether user has entered the allowed characters or not.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    var regex = new Regex(@"[^a-zA-Z0-9s]");
    if (regex.IsMatch(e.KeyChar.ToString()))
    {
        e.Handled = true;
    }
}

但是我们必须在每个文本框按键事件上实现它(如果没有其他解决方案,这是最后要做的事情).有没有办法从一个地方处理这个并影响每个文本框(在某些地方文本框也有自己的按键事件).我需要的是一种通用方法,它将在任何文本框的每个按键事件上触发.

but then we have to implement this on every textboxes key press events ( if there is no other solution this is the last thing to do). Is there any way to handle this from a single place and affect for every textboxes (on some places textboxes have their own key press events as well). What I need is a common method which will be fire on every key press events of any textbox.

解决方案:创建一个从 TextBox(或 TextBoxBase)派生的自定义控件,其中包含我的验证所需的所有逻辑,因此所有这些都在一个地方完成.但是我仍然必须再次将所有现有的文本框更改为我的新文本框.有没有办法改变当前事件处理程序的行为?

Solution : Create a Custom Control derived from TextBox (or TextBoxBase) that contains all the logic required for my validation, so it's all done in one place. But still I have to again change all the existing textboxes my new textbox. Is there any way to change behavior of current event handler?

注意:我需要的是覆盖文本框的当前按键事件并运行我的验证代码,如果按键事件中有任何明确提到的代码,则需要运行.

推荐答案

如果你想将 KeyDown 事件添加到所有 TextBoxes 你可以循环它们并添加所有的 EventHandler 都相同.

If you want to add the KeyDown Event to all TextBoxes you can loop through them and add the same EventHandler for all of them.

为此,首先我们需要创建一个循环遍历所有文本框的函数.

To do that first of all we need to create a function that loop through all our TextBoxes.

GetChildControls 函数:

public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) 
    where TControl : Control
{
    var children = (control.Controls != null) ? 
        control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>();
    return children.SelectMany(c => GetChildControls<TControl>(c)).Concat(children);
}

我们现在可以在 InitializeComponent(); 之后使用该函数将 Txt_KeyDown() EventHandler 分配给所有 TextBox.

We can now use that function after the InitializeComponent(); to assign the Txt_KeyDown() EventHandler to all TextBoxes.

调用函数:

public Example() {
    InitializeComponent();
    var allTextBoxes = this.GetChildControls<TextBox>();
    foreach (TextBox tb in allTextBoxes)
    {
        tb.KeyDown += Txt_KeyDown;
    }
}

private void Txt_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) {
    // Your code here
}

这篇关于防止在文本框中输入特殊字符(解决方案中已经有数千个文本框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)