How to keep valid value in NumericUpDown instead assigning Maximum value?

╄→гoц情女王★ 提交于 2020-01-04 03:49:14

问题


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.


回答1:


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);
}



回答2:


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;
   }
}



回答3:


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();
    }
}



回答4:


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.



来源:https://stackoverflow.com/questions/12142727/how-to-keep-valid-value-in-numericupdown-instead-assigning-maximum-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!