简体   繁体   中英

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.

Currently, I am just using a TextChanged method for getting values, which then make other buttons/checkboxes show/hide based on the entry.

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). This does work with the TextChanged method.

I have a regex set that utilizes the PreviewTextInput and KeyDown methods. 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).

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

For an Int it is as easy as just binding to an Int.

If you need to be able to increase/decrease the value via button use NumericUpDown or one of its subclass.

If you just need a textbox, you have to handle PreviewKeyDown() event. You need to manually check for valid/invalid keys pressed. When an invalid key is pressed, you set 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:

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

And txtAddNumber_PreviewTextInput method:

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.

And your txtAddLable_TextChanged method will bw like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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