简体   繁体   中英

help me to cheat with numericUpDown1_ValueChanged event

I have a Form and numericupdown control located on it. I want that in some conditions (_condition1) user cannot be able to change a value of numericupdown control. How can I do it ? I wrote some code but it works twice (double time).

class Form1 : Form
{
    bool _condition1;

    int _previousValue;

    void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
           if(_condition1)
           {
                 numericUpDown1.Value = (decimal)_previousValue;
           }
           else
           {
                 _previousValue = (int)numericUpDown1.Value;
           }
    }
}

Control must be enable.

numericUpDown1.ReadOnly = true;

?

.... edit: ..........

Another (ugly) solution would be to remove the event, change the value, and add the event again ..

numericupdown1.ValueChanged -= new EventHandler ....
numericupdown1.Value = value;
numericupdown1.ValueChanged += new EventHandler ....

Have you tried using the Validating event?

EDIT #1 Have you tried the Leave event?

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