简体   繁体   English

用于检查C#中的数字的TextBox方法

[英]TextBox method for checking numerics in C#

I am creating a program that has a lot of user inputs. 我正在创建一个有很多用户输入的程序。 Most of the user inputs are going to be in TextBoxes that need to be only numeric entries. 大多数用户输入将在TextBox中,只需要数字条目。

Currently, I am just using a TextChanged method for getting values, which then make other buttons/checkboxes show/hide based on the entry. 目前,我只是使用TextChanged方法获取值,然后根据条目使其他按钮/复选框显示/隐藏。

I am wanting to create a method or implement some kind of utilization that checks when is being inputted into the boxes, to either prevent people from making incorrect inputs, to fix changes that they had made, or to create a messagebox that will tell them that their input is invalid. 我想创建一个方法或实现某种利用率,检查何时输入框,防止人们输入错误,修复他们所做的更改,或创建一个消息框,告诉他们他们的输入无效。

I have two ways I am currently working with but they don't work with each other. 我目前有两种方式,但他们不相互合作。

I have a parse method, that converts the input text into a Double but the problem I am running into, if they utilize the backspace button then re-enter their numbers, it will not recognize the input (which is needed to open/close other textboxes/checkboxes). 我有一个解析方法,将输入文本转换为Double但我遇到的问题,如果他们使用退格按钮然后重新输入他们的数字,它将无法识别输入(这是打开/关闭其他所需的文本框/复选框)。 This does work with the TextChanged method. 这适用于TextChanged方法。

I have a regex set that utilizes the PreviewTextInput and KeyDown methods. 我有一个使用PreviewTextInput和KeyDown方法的正则表达式集。 This works pretty well with not allowing certain inputs but it doesn't work with the textchanged method (or at least I don't understand how to point to it). 这非常适用于不允许某些输入,但它不适用于textchanged方法(或者至少我不明白如何指向它)。

I am in need of some guidance on how to create a viable method for checking inputs into textboxes that doesn't require my users to press a button for each entry (aka checking real-time). 我需要一些指导如何创建一个可行的方法来检查文本框中的输入,这不需要我的用户按下每个条目的按钮(也就是检查实时)。

I think this is what you are looking for. 我想这就是你要找的东西。
Binding.Validation Binding.Validation

For an Int it is as easy as just binding to an Int. 对于Int来说,它就像绑定到Int一样简单。

If you need to be able to increase/decrease the value via button use NumericUpDown or one of its subclass. 如果您需要能够通过按钮增加/减少值,请使用NumericUpDown或其子类之一。

If you just need a textbox, you have to handle PreviewKeyDown() event. 如果您只需要一个文本框,则必须处理PreviewKeyDown()事件。 You need to manually check for valid/invalid keys pressed. 您需要手动检查按下的有效/无效键。 When an invalid key is pressed, you set e.Handled = true; 按下无效键时,设置e.Handled = true; to prevent the key down event from tunneling down. 防止关键事件发生隧道效应。

I really couldn't understand completely, but according to me you are trying to prevent a textbox to take invalid input and at the same time you want to use TextChanged method, so you can do like this: 我真的无法完全理解,但据我说,你试图阻止文本框采取无效输入,同时你想使用TextChanged方法,所以你可以这样做:

<TextBox Name="txtAddNumber" TextChanged="txtAddLable_TextChanged" PreviewTextInput="txtAddNumber_PreviewTextInput" />

And txtAddNumber_PreviewTextInput method: 和txtAddNumber_PreviewTextInput方法:

private void txtAddNumber_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    char c = Convert.ToChar(e.Text);
    if (!Char.IsLetter(c))
        e.Handled = false;
    else
        e.Handled = true;
    base.OnPreviewTextInput(e);
}

And if you want to handle some error message kind of thing on the base of input you can do like this: 如果你想在输入的基础上处理一些错误消息,你可以这样做:

private void txtAddNumber_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    char c = Convert.ToChar(e.Text);
    if (!Char.IsLetter(c))
    {
        // Put your Logic here according to requirement
        e.Handled = false;
    }
    else
    {
        // Put your Logic here according to requirement
        e.Handled = true;
    }
    base.OnPreviewTextInput(e);
}

And

e.Handled = false means input is numeric and e.Handled = true means input is non-numeric. e.Handled = false表示输入为数字, e.Handled = true表示输入为非数字。

And your txtAddLable_TextChanged method will bw like: 你的txtAddLable_TextChanged方法会像:

private void txtAddLable_TextChanged(object sender, TextChangedEventArgs e)
{
      // Logics here...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM