开发者

Retrieving "invalid" value from a NumericUpDown Validating event

开发者 https://www.devze.com 2022-12-31 11:51 出处:网络
When the user enters a value above 开发者_StackOverflow中文版numericUpDown.Maximum, the control\'s value is automatically set to the maximum. I\'d like to display a MessageBox when this occurs, but I\

When the user enters a value above 开发者_StackOverflow中文版numericUpDown.Maximum, the control's value is automatically set to the maximum. I'd like to display a MessageBox when this occurs, but I'm not able to do that because control.Value and control.Text already contain the automatically set value, maximum, when Validating event is raised.

private void numericUpDown_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
       NumericUpDown control = sender as NumericUpDown;
       decimal newValue = control.Value;

       // decimal newValue;
       // decimal.TryParse(control.Text, out newValue)

       if (newValue > control.Maximum || newValue < control.Minimum)
       {
            // MessageBox

        }

}

Thanks


Nagging the user by slapping her with message boxes doesn't make for the greatest user interface. But you can easily do it just by setting the min and max smaller/larger and checking the value in the ValueChanged event.

0

精彩评论

暂无评论...
验证码 换一张
取 消