Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to signal WinForm control input out of range?

Status
Not open for further replies.

thedougster

Programmer
Jan 22, 2009
56
US
Is there a way (perhaps via EventArgs) to signal the app when a user tries to enter out-of-range input into a WinForm control? (Specifically, a number in a NumericUpDown control numericUpDown1 that is larger than numericUpDown1.Maximum?) Yes, I know the control won't allow the input, but I want to display an error message if this happens.
 
Easy...

1: Create your own control, inherit from NumericUpDown

2: Create your own private field to hold this control's maxvalue

3: Add a public property for Maximum, Get = your private field, Set = (a) your private field, (b) your control's base class's Maximum to Decimal.MaxValue

4: Add a Handler for the TextChanged Event. Use Decimal.TryParse to validate the input and check it against your private maximim value. If it is within range then no problem otherwise set your control's Value property to the private field and throw an appropriate exception.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top