【问题标题】:Validate on text change in TextBox验证 TextBox 中的文本更改
【发布时间】:2010-10-11 06:37:09
【问题描述】:

我已经在我的 WinForm 中的文本框上实现了验证规则,它运行良好。但是,它仅在我退出该字段时检查验证。我希望它在框中输入任何内容以及每次内容更改时立即进行检查。我还希望它在 WinForm 打开后立即检查验证。

我记得最近通过设置一些事件和诸如此类的东西来做到这一点,但我似乎不记得是如何做到的。

【问题讨论】:

    标签: c# .net winforms validation textbox


    【解决方案1】:

    如果您使用数据绑定,请转到文本框的属性。打开顶部的(DataBindings),点击(Advanced)属性,会出现三个点(...)点击那些。出现高级数据绑定屏幕。对于绑定的 TextBox 的每个属性,在您的情况下为 Text,您可以使用组合框 Data Source Update mode 设置数据绑定以及验证何时“启动”。如果您将其设置为OnPropertyChanged,它将在您键入时重新评估(默认为OnValidation,它只会在您选择时更新)。

    【讨论】:

    • 最快、最简单、最干净的解决方案
    【解决方案2】:

    TextChanged 事件

    以后您可以在 MSDN 库中找到所有事件,这里是 TextBox class reference

    http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox(VS.80).aspx

    【讨论】:

    • TextChanged 事件只会在焦点从控件中丢失时触发(例如从选项卡出来)。这只会防止您离开后输入无效数据。 OP 似乎想阻止它被允许使用。
    • 这不准确,TextChanged 无论焦点如何都会触发。
    【解决方案3】:

    如果没有完成,您的数据将如何有效?即用户键入一个数字,您尝试将其验证为日期?

    【讨论】:

    • 我的猜测是它是一个不能包含字母的数字字段,反之亦然
    • 如果您想防止人们输入无效字符,这很常见。如果您检查 KeyPress 或 KeyDown,您可以捕获输入并使其永远不会出现。
    • 这就是重点。数据未完成时无效,因此我们希望在字段包含无效数据时出现错误。这包括在启动时和键入时,只要数据仍然无效。
    • 好吧,如果您不介意告诉用户他们的输入无效,这很公平,但如果系统在我开始输入时告诉我我的数据无效,我个人不会留下深刻印象。跨度>
    • ck:不需要告诉你。您可以轻松编写代码以防止出现这些非法字符,这样如果您开始敲打“123abc4565”,您的“abc”就不会出现,因为代码在幕后处理了它。
    【解决方案4】:

    将文本框绑定到 bindingSource 时,转到 Advanced 并选择验证类型
    “关于属性改变”。这将在每次按键时将您的数据传播到您的实体。 Here is the screen shot

    【讨论】:

      【解决方案5】:

      您应该检查 KeyPress 或 KeyDown 事件,而不仅仅是您的 TextChanged 事件。

      这是直接来自MSDN documentation 的 C# 示例:

      // Boolean flag used to determine when a character other than a number is entered.
      private bool nonNumberEntered = false;
      
      // Handle the KeyDown event to determine the type of character entered into the control.
      private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
      {
          // Initialize the flag to false.
          nonNumberEntered = false;
      
          // Determine whether the keystroke is a number from the top of the keyboard.
          if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
          {
              // Determine whether the keystroke is a number from the keypad.
              if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
              {
                  // Determine whether the keystroke is a backspace.
                  if(e.KeyCode != Keys.Back)
                  {
                      // A non-numerical keystroke was pressed.
                      // Set the flag to true and evaluate in KeyPress event.
                      nonNumberEntered = true;
                  }
              }
          }
          //If shift key was pressed, it's not a number.
          if (Control.ModifierKeys == Keys.Shift) {
              nonNumberEntered = true;
          }
      }
      
      // This event occurs after the KeyDown event and can be used to prevent
      // characters from entering the control.
      private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
      {
          // Check for the flag being set in the KeyDown event.
          if (nonNumberEntered == true)
          {
              // Stop the character from being entered into the control since it is non-numerical.
              e.Handled = true;
          }
      }
      

      【讨论】:

      • 我认为你也应该检查 TextValueChanged,因为有人可以简单地“粘贴”非法字符 :)
      • 此方法不适用于粘贴(它会阻止 CTRL-V,但不会检查文本框上下文菜单“粘贴”)。您需要单独处理。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      相关资源
      最近更新 更多