C# 验证 TextBox 中的文本更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/610710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:13:05  来源:igfitidea点击:

Validate on text change in TextBox

c#.netwinformsvalidationtextbox

提问by Sakkle

I have implemented validation rules on a textBox in my WinForm and it works well. However it checks the validation only when I tab out of the field. I would like it to check as soon as anything is entered in the box and everytime the content changes. Also I'd like it to check validation as soon as the WinForm opens.

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

I remember doing this fairly recently by setting some events and whatnot, but I can't seem to remember how.

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

采纳答案by Jason

TextChanged event

文本更改事件

in the future you can find all of the events on the MSDN library, here's the TextBox class reference:

将来您可以在 MSDN 库中找到所有事件,这里是TextBox 类参考

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

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

回答by TheTXI

You should be checking on KeyPress or KeyDown events and not just your TextChanged event.

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

Here is a C# Example direct from the MSDN documentation:

这是直接来自MSDN 文档的 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;
    }
}

回答by cjk

How will your data be valid if it isn't finished? i.e. a user types a number and you try and validate it as a date?

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

回答by Kurt Schelfthout

If you're using databinding, go to the Properties of the textbox. Open (DataBindings) at the top, click on the (Advanced) property, three dots will appear (...) Click on those. The advanced data binding screen appears. For each property of the TextBox that is bound, in your case Text, you can set when the databinding, and thus the validation, should "kick in" using the combobox Data Source Update mode. If you set it to OnPropertyChanged, it will re-evaluate as you type (the default is OnValidationwhich only updates as you tab).

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

回答by Valentin Vasilyev

When binding your textbox to a bindingSource go to Advanced and select validation type
"On Property Changed". This will propagate your data to your entity on each key press. Here is the screen shot

将文本框绑定到 bindingSource 时,转到 Advanced 并选择验证类型
“On Property Changed”。这将在每次按键时将您的数据传播到您的实体。 这是屏幕截图