tags:

views:

49

answers:

2

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.

+1  A: 

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 ....
Run CMD
Well, it doesn't fit me. It blocks an user input and makes a grey backgorund color...
alex
The reason it fires twice, is because when the event gets fired, and condition 1 is true, you change the value yourself, which fires the 'ValueChanged' event again. ..
Run CMD
Also, you could set readonly true, and BackColor white ... gives the same look
Run CMD
Thanks! I will use the solution with += and -=
alex
A: 

Have you tried using the Validating event?

EDIT #1 Have you tried the Leave event?

Will Marcouiller
I tried but it didn't fired... I don't know why ...
alex