how to signal WinForm control input out of range?

Status
Not open for further replies.

BobLewiston

In Runtime
Messages
182
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.
 
You just need to handle the appropriate event (probably the LostFocus event) and display a message box in that method.
 
jaeusm:

This sounds like the right approach, but when I created an event handler numEnter_LostFocus, it doesn't get invoked by entering out-of-range input into NumericUpDown control numEnter;
 
it doesn't get invoked by entering out-of-range input into NumericUpDown control numEnter
Correct. As the name of the event implies, it gets invoked when the control loses focus, which is when the value would be set anyway.
 
Correct. As the name of the event implies, it gets invoked when the control loses focus, which is when the value would be set anyway.

I guess I don't understand what it means to "lose focus". Does that mean when the flow of control passes from the WinForm control?
 
Only one item can have focus on the screen at any given time. The focused item can accept keyboard input, which is why only one item can have focus at a time. It doesn't make any sense for a control that accepts keyboard input to validate the value until the user is done entering it from the keyboard. The only way a control can know that the user is finished entering characters is when it loses focus. For a control to lose focus, some other item on the screen must 'get' focus.
 
jaeusm:

Suppose you want the NumericUpDown control to consider the number input by the user to be officially "entered" when the user presses the Enter key? The NumericUpDown control already behaves that way, and I don't want to change that. But in such a case, the focus is still on the NumericUpDown control.
 
As I mentioned before, you need to handle the appropriate event. Just look at the available events. In this case, use the ValueChanged event. You could also handle the KeyDown event and check to see if enter was pressed.
 
Status
Not open for further replies.
Back
Top Bottom