1

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).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Duck
  • 13
  • 3
  • 1
    Is this a WinForms application? If yes why not use the NumericUpDown control? – Steve Jun 30 '16 at 22:43
  • This is in WPF. My apologies for not being specific. – Duck Jun 30 '16 at 22:47
  • This is sample from Microsoft : https://msdn.microsoft.com/en-us/library/ms229644(v=vs.100).aspx You can consider also using MaskedTextBox –  Jun 30 '16 at 22:48
  • You could perhaps validate the value when the textbox loses focus. If you need to validate the input without requiring the user to leave the textbox, you may need to reset a timer on each `TextChanged` event, and then run your validation routine when the timer elapses. This would allow the user a second or so between keystrokes to finish their typing. – wablab Jun 30 '16 at 22:48
  • There are similar controls for WPF http://stackoverflow.com/questions/382676/good-numericupdown-equivalent-in-wpf – Steve Jun 30 '16 at 22:49
  • NumericUpDown is truly the a good way. – alex10 Jul 01 '16 at 05:32
  • @Stanley thanks for the example. – Duck Jul 01 '16 at 15:21

3 Answers3

2

I think this is what you are looking for.
Binding.Validation

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

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • I am having a hard time to wrap my head around the implementation of this feature. I will preface with, I am very new with C# and XAML, but I have had a few Java classes under my belt. The structure in which they give examples doesn't fully translate into what I actually am doing, so bare with me but I am just not seeing how it is implemented in a WPF. – Duck Jul 01 '16 at 22:51
  • Well they are probably using proper structures so I suggest you copy. Bind to a object from a class with public properties is best practice in WPF. In the changed event is NOT best practice. https://msdn.microsoft.com/en-us/vstudio/ms753962(v=vs.92).aspx – paparazzo Jul 01 '16 at 23:53
  • Would each textbox need it's own class, essentially? – Duck Jul 05 '16 at 21:55
  • Own class? No. You need to get familiar with binding. – paparazzo Jul 05 '16 at 22:03
1

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.

Jai
  • 8,165
  • 2
  • 21
  • 52
0

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...
}
Suman Kumar
  • 305
  • 1
  • 13