3

Say I have NumericUpDown with Maximum = 99 and Minimum = -99 and initial value = 23. If user sets focus to this contol and inputs 1 (that would be 123 now) it changes it's value to 99. How do I keep 23 instead changing value to maximum allowed?

I tried to catch KeyDown and KeyPress, but value wasn't changed during this events. Also I tried to implement a workaround explained in this question, but not succeeded. Validating event occurs only on leaving control. I need to simply ignore user input if it's greater than Maximum or lesser than Minimum.

UPD. I'm using WinForms.

0

4 Answers 4

4

Use an outside global property like private int iTextBox { get; set; } and use OnTextChange event to see if the number is bigger then 99 or smaller than -99.

OnTextChange:

{
       int newValue = int.Parse(textBox1.Text);
       if (newValue > Maximum)
              textBox1.Text = iTextBox;
       if (newValue < Minimum)
              textBox1.Text = iTextBox;

       iTextBox = int.Parse(textBox1.Text);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Ok, I found solution with this question help. I tried many combinations and found one that wasn't too complicated. I save old value on KeyDown event and check it on textBox.TextChanged event. At that time value haven't changed yet. Now numericUpDown visually discards input that will be not in Minimum...Maximum range. Not user-friendly I think, there is some work to do.

public partial class Form1
{
   private decimal _oldValue;
   private TextBox textBox;

   public Form1()
   {
      InitializeComponent();

      textBox = (TextBox)numericUpDown.Controls[1];
      textBox.TextChanged += TextBoxOnTextChanged;
   }

   private void TextBoxOnTextChanged(object sender, EventArgs eventArgs)
    {
        decimal newValue = Convert.ToDecimal(((TextBox) sender).Text);
        if (newValue > numericUpDown.Maximum || newValue < numericUpDown.Minimum)
            ((TextBox) sender).Text = _oldValue.ToString();
    }

   private void numericUpDown_KeyDown(object sender, KeyEventArgs e)
   {
      _oldValue = ((NumericUpDownCustom) sender).Value;
   }
}

Comments

0

If you're using WPF then write a converter which will re-assign a value for you.

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType,  object parameter, CultureInfo culture)
    {
        int i = int.Parse(value as string);
        // logic here
    }

    public object ConvertBack(object value, Type targetType,  object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Comments

0

Can't you do this in the ValueChanged event of the NumericUpDown control? Just store the original value and if the value they are entering is invalid, restore the saved value.

3 Comments

No. Because when you catch ValueChanged, event controls value already changed to new that already cut.
@JohnPreston - Understood, but you could use the Enter or Focus events to store the current value of the control. Or better yet, don't use the control as the storage for the value, have a class that holds these values that is updated when they change. The control should only display the value.
How do I differ valid 99 value that user entered himself and corrected 99 from 123 value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.