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.

Recommended Answers

All 13 Replies

I would suggest something like that you would want to be an exception rather than event

Create an exception class and do a check before passing the value to the numeric control and throw an exception if out of bounds

I would suggest something like that you would want to be an exception rather than event

Create an exception class and do a check before passing the value to the numeric control and throw an exception if out of bounds

How can I do that? When a user is entering input into a WinForm Control, the flow of control in the program is inside of the WinForm Control, which is just a black box to the app.

If I understand you correctly, I would have to dummy up my own custom version of a NumericUpDown control, screen the input, and then pass acceptable input to the real NumericUpDown control.

That would just circumvent the whole ease of use of the real NumericUpDown control. Surely Microsoft would have seen this one coming and provided a way to deal with the problem.

Oh i thought you were making your own control

You should be able to catch the validating event and get the value before it has changed back to maximum

You should be able to catch the validating event and get the value before it has changed back to maximum

How?

In the designer click on the events for the numeric control you are trying to catch the events for and double click on validating

Or on the codeside
numericcontrol.Validating += eventnamehere

In the designer click on the events for the numeric control you are trying to catch the events for and double click on validating

Or on the codeside
numericcontrol.Validating += eventnamehere

OK, I entered "numEnterRadius_OutOfRange" in NumericUpDown control numEnterRadius's Validating property, and I created this event handler in Form1.cs:

private void numEnterRadius_OutOfRange (object sender, CancelEventArgs e)
{
    if (numEnterRadius.Value < 0 || 
        numEnterRadius.Value > numEnterRadius.Maximum)
        MessageBox.Show ("Input out of range!", "Duh..!");
}

and of course this line was generated in Form1.Designer.cs:

this.numEnterRadius.Validating += new System.ComponentModel.CancelEventHandler (this.numEnterRadius_OutOfRange);

but no matter what bad input I give it, the MessageBox only appears when I comment out

if (numEnterRadius.Value < 0 || numEnterRadius.Value > numEnterRadius.Maximum)

so the NumericUpDown control is apparently just not seeing that either one of those conditions is being met.

Sorry to be so ignorant, but I'm a newbie.

Yeh that didn't quite work as i thought it would have

What you can do instead is capture previewkeydown
add this to the existing field

"12" + "4"

124

then check 124 to be greater than the max

dickersonka:

I'm sorry, I don't understand at all.

first add the event for PreviewKeyDown the same way as you did for validating

here is the code i would use inside the event
i'm not always that great at explaining things like this, and hopefully this will be more clear

//We get the key pressed
            KeysConverter kc = new KeysConverter();
            string numString = kc.ConvertToString(e.KeyCode);
            int num = -1;

            if (Int32.TryParse(numString, out num))
            {
                //Get the current value 
                string curVal = this.numericUpDown1.Value.ToString();
                //Set our new value
                curVal += num.ToString();

                //Now we check the new val against our control
                if (Int32.TryParse(curVal, out num))
                {
                    if (num > this.numericUpDown1.Maximum || num < this.numericUpDown1.Minimum)
                    {
                        MessageBox.Show(curVal + " is out of the range");
                    }
                }
            }

dickersonka:

When does this code get executed? The first time the user presses a key while using the control? Because if so, suppose the user doesn't just press the Enter key upon scrolling to the desired value (which might not even be possible, depending upon the increment and the desired value, especially for floating point numbers), but rather types in the desired value?

This happens when the control has focus and the user presses a key

I use this here
if (Int32.TryParse(numString, out num))

to check if it was a numeric key that was pressed, if not, it is ignored

dickersonka:

Please pardon an ignorant question: does the control "having focus" mean that the flow of control within the program is currently in the code pertaining to the control; in other words, that the user is currently using the control?

I mean that if you are typing for the control to receive the typed in text, then this event will get caught

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.