I've got a numbers-only TextEdit box with MaxLength
set to 2, allowing the user to enter 0-99. Can I restrict this to a smaller range, say 0-30, without catching the TextChanged
event, validating the input and showing a messsag开发者_StackOverflow中文版e (or similar method)?
You should use the SpinEdit
control and set the max and min properties.
You could set the regexp mask to \d|[0-2]\d|30
.
Handle the EditValueChanging event,
private void txtQuantity_EditValueChanging(object sender, ChangingEventArgs e)
{
var value = Convert.ToDouble(e.NewValue);
if (value < MIN || value > MAX) e.Cancel = true;
}
In winforms you should use System.Windows.Forms.NumericUpDown and set the Maximum and Minimum properties.
精彩评论