Reputation: 1149
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.
Upvotes: 0
Views: 727
Reputation: 24132
Have you tried using the Validating event?
EDIT #1 Have you tried the Leave event?
Upvotes: 0
Reputation: 3035
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 ....
Upvotes: 1