How to hold the invalid value for NumericUpDown after it loses focus?

前端 未结 1 1885
说谎
说谎 2020-12-11 05:23

In my project there is an UserControl which includes a NumericUpDown ctrl, and its valid value range is from 10 to 100,

so if user inputs 200 in Nu

相关标签:
1条回答
  • 2020-12-11 05:44

    The trick is to get the textbox embedded within the numeric updown control, and handle its Validating event.

    Here is how to get it done:

    Creat a dummy form and add a numeric updown control and some other controls, and when the numeric unpdown control looses the focus, the text of the form will be set to the value that the user entered.

    Here it the code of what I have done:

    public partial class Form1 : Form
        {
            TextBox txt;
            public Form1()
            {
                InitializeComponent();
                txt = (TextBox)numericUpDown1.Controls[1];//notice the textbox is the 2nd control in the numericupdown control
                txt.Validating += new CancelEventHandler(txt_Validating);
            }
            void txt_Validating(object sender, CancelEventArgs e)
            {
                this.Text = txt.Text;
            }
        }
    

    EDIT:

    @Carlos_Liu: Ok, I can see now the problem, you can achieve this with the TextChanged event, just save the value in a dummy variable and reuse it at txt_Validating, but be cautious, don't update this variable unless the textbox is focused.

    Here is the new sample code:

    public partial class Form1 : Form
    {
        TextBox txt;
        string val;
        public Form1()
        {
            InitializeComponent();
            txt = (TextBox)numericUpDown1.Controls[1];//notice the textbox is the 2nd control in the numericupdown control
            txt.TextChanged += new EventHandler(txt_TextChanged);
            txt.Validating += new CancelEventHandler(txt_Validating);
        }
    
        void txt_TextChanged(object sender, EventArgs e)
        {
            if (txt.Focused) //don't save the value unless the textbox is focused, this is the new trick
                val = txt.Text;
        }
        void txt_Validating(object sender, CancelEventArgs e)
        {
            MessageBox.Show("Val: " + val);
        }
    }
    

    EDIT#2

    @Carlos_Liu: If you need the entered value to be kept, still there is a trick for doing so: @ the Validating event of the textbox, check the value, if it is not within the range, cancel loosing the focus!

    Here is a new version of the code:

    public partial class Form1 : Form
    {
        TextBox txt;
        string val;
        public Form1()
        {
            InitializeComponent();
            txt = (TextBox)numericUpDown1.Controls[1];
            txt.TextChanged += new EventHandler(txt_TextChanged);
            txt.Validating += new CancelEventHandler(txt_Validating);
        }
    
        void txt_TextChanged(object sender, EventArgs e)
        {
            if (txt.Focused)
                val = txt.Text;
        }
        void txt_Validating(object sender, CancelEventArgs e)
        {
            int enteredVal = 0;
            int.TryParse(val, out enteredVal);
            if (enteredVal > numericUpDown1.Maximum || enteredVal < numericUpDown1.Minimum)
            {
                txt.Text = val;
                e.Cancel = true;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题