• <tfoot id='eAz3T'></tfoot>

    <small id='eAz3T'></small><noframes id='eAz3T'>

  • <i id='eAz3T'><tr id='eAz3T'><dt id='eAz3T'><q id='eAz3T'><span id='eAz3T'><b id='eAz3T'><form id='eAz3T'><ins id='eAz3T'></ins><ul id='eAz3T'></ul><sub id='eAz3T'></sub></form><legend id='eAz3T'></legend><bdo id='eAz3T'><pre id='eAz3T'><center id='eAz3T'></center></pre></bdo></b><th id='eAz3T'></th></span></q></dt></tr></i><div id='eAz3T'><tfoot id='eAz3T'></tfoot><dl id='eAz3T'><fieldset id='eAz3T'></fieldset></dl></div>

  • <legend id='eAz3T'><style id='eAz3T'><dir id='eAz3T'><q id='eAz3T'></q></dir></style></legend>
        • <bdo id='eAz3T'></bdo><ul id='eAz3T'></ul>

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

        To prevent entering special characters on textbox ( Already have thousand of textboxes in solution )(防止在文本框中输入特殊字符(解决方案中已经有数千个文本框))
        • <small id='fCdWv'></small><noframes id='fCdWv'>

        • <tfoot id='fCdWv'></tfoot>

            1. <i id='fCdWv'><tr id='fCdWv'><dt id='fCdWv'><q id='fCdWv'><span id='fCdWv'><b id='fCdWv'><form id='fCdWv'><ins id='fCdWv'></ins><ul id='fCdWv'></ul><sub id='fCdWv'></sub></form><legend id='fCdWv'></legend><bdo id='fCdWv'><pre id='fCdWv'><center id='fCdWv'></center></pre></bdo></b><th id='fCdWv'></th></span></q></dt></tr></i><div id='fCdWv'><tfoot id='fCdWv'></tfoot><dl id='fCdWv'><fieldset id='fCdWv'></fieldset></dl></div>

                  <tbody id='fCdWv'></tbody>
                <legend id='fCdWv'><style id='fCdWv'><dir id='fCdWv'><q id='fCdWv'></q></dir></style></legend>
                  <bdo id='fCdWv'></bdo><ul id='fCdWv'></ul>
                  本文介绍了防止在文本框中输入特殊字符(解决方案中已经有数千个文本框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们有一个使用 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
                  }
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to keep the Text of a Read only TextBox after PostBack()?(PostBack()之后如何保留只读文本框的文本?)
                  Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word(Winforms 文本框 - 使用 Ctrl-Backspace 删除整个单词)
                  C# - Add button click events using code(C# - 使用代码添加按钮单击事件)
                  Multi-color TextBox C#(多色文本框 C#)
                  How can i set the caret position to a specific index in passwordbox in WPF(如何将插入符号位置设置为 WPF 密码框中的特定索引)
                  C# Numeric Only TextBox Control(C# 纯数字文本框控件)

                    <i id='UdEup'><tr id='UdEup'><dt id='UdEup'><q id='UdEup'><span id='UdEup'><b id='UdEup'><form id='UdEup'><ins id='UdEup'></ins><ul id='UdEup'></ul><sub id='UdEup'></sub></form><legend id='UdEup'></legend><bdo id='UdEup'><pre id='UdEup'><center id='UdEup'></center></pre></bdo></b><th id='UdEup'></th></span></q></dt></tr></i><div id='UdEup'><tfoot id='UdEup'></tfoot><dl id='UdEup'><fieldset id='UdEup'></fieldset></dl></div>

                    1. <legend id='UdEup'><style id='UdEup'><dir id='UdEup'><q id='UdEup'></q></dir></style></legend>
                        <tbody id='UdEup'></tbody>

                      <small id='UdEup'></small><noframes id='UdEup'>

                          • <bdo id='UdEup'></bdo><ul id='UdEup'></ul>
                            <tfoot id='UdEup'></tfoot>