Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

how to signal WinForm control input out of range?


  • Please log in to reply
15 replies to this topic

#1 BobLewiston

BobLewiston

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:11:24 PM

Posted 05 March 2009 - 03:01 PM

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.

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:24 PM

Posted 05 March 2009 - 03:11 PM

Hello :thumbsup:

Handle whichever event corresponds to a change in your control. For example:

(This is VB pseudocode... not sure how C# handles events)

Private Sub someFunc (byval e as someeventobject) Handles numericUpDown1.SelectedIndexChanged
if numericUpDown1.SelectedItem <> // some condition with your range
msgbox("Invalid value for that box!")
endif
End Sub

Not entirely firmiliar with numericUpDown but that's the general idea.

EDIT: I believe the event you need to handle is ValueChanged:
http://msdn.microsoft.com/en-us/library/sy...ged(VS.71).aspx

Billy3

Edited by Billy O'Neal, 05 March 2009 - 03:12 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 BobLewiston

BobLewiston
  • Topic Starter

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:11:24 PM

Posted 05 March 2009 - 03:40 PM

Billy O'Neal:

This won't work because the NumericUpDown control won't let a user change the value to one which is out of range. I just want the control to be able to signal the app that such an attempt had been made, even though the attempt couldn't possibly succeed.

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:24 PM

Posted 05 March 2009 - 04:16 PM

EDIT: I believe the event you need to handle is ValueChanged:



#5 BobLewiston

BobLewiston
  • Topic Starter

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:11:24 PM

Posted 05 March 2009 - 04:48 PM

groovicus:

The event ValueChanged occurs in the NumericUpDown control when the Enter key is pressed. The NumericUpDown control will let a negative value or a value greater than numericUpDown1.Maximum be typed (or copied) into it, but when you press the Enter key, the control won't actually accept such a value and assign it to numericUpDown1.Value.

If you try to enter too large a value, the control will just set numericUpDown1.Value equal to numericUpDown1.Maximum, and if you try to enter a negative value, it will just set numericUpDown1.Value to zero.

So the fact that an attempt was made to enter an inadmissible value never exceeds the scope of the control to be known to the app.


#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:24 PM

Posted 05 March 2009 - 05:16 PM

I guess I am not understanding. You want an error message displayed if the input is out of range, but you want that to happen before the control is even aware it has input? Could you describe a use case so I understand better what you want.

#7 BobLewiston

BobLewiston
  • Topic Starter

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:11:24 PM

Posted 05 March 2009 - 05:48 PM

The control is not telling the app that out-of-range input has been entered. It's just changing the value entered so it's in range and making that changed value available to the app via the control's Value property, even though this changed value is not what the user entered. And of course this can't happen, nor do I want it to happen, before the control is even aware it has input.

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:24 PM

Posted 05 March 2009 - 07:11 PM

I edited your post there... you must have accidentally made the text oversized, or perhaps thought that if you made it bigger, I would understand better. I asked for a simple use case so that I could better understand what you want to happen, because it is not at all clear to me what you want. I can not help if I do not understand. My guess is that you want this to happen:

1. User inputs a value
2. Control realizes that the value is out of range
3. Display an error message.

The only event of which I am aware that will even look is the ValueChanged event.

Here is my code that I used. I set the bounds via the properties window first:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
		{
			if (numericUpDown1.Value > 10 || numericUpDown1.Value < 0)
			{
				Console.Write("Error. Number must be between 1 and 10");
				numericUpDown1.Value = 1;
				numericUpDown1.ReadOnly = true;
			}
		}

It works perfectly. Of course, the easiest thing to do is to make the box so that it is not directly editable in the first place, alleviating the need for an error message at all.
numericUpDown1.ReadOnly = true;

Am I still not understanding what you are trying to do?

#9 BobLewiston

BobLewiston
  • Topic Starter

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:11:24 PM

Posted 05 March 2009 - 08:41 PM

groovicus:

If a user enters a number into a NumericUpDown control that is greater than the control's Maximum property, the control changes the number input to a value equal to that of the control's Maximum property. Flow of control then exits from this control and proceeds to the event-handler method.

Therefore, unless the maximum value allowed by the event-handler is less than that of the control's Maximum property, the event-handler can't possibly even detect the input of too large a number.

In addition, it seems quite ineloquent to have the number input by the user to be compared to TWO values: the value that the user's input was reduced to by the control, and the maximum value allowed by the event handler. By definition, a simple (non-compound) comparison, such as comparing the user's input to an allowed maximum, or to an allowed minimum, should involve a comparison to only ONE value.

Furthermore, having two values other than the user input involved would make modification of the code more cumbersome if it is later decided to change the maximum allowed value.

Moreover, even if an error is signaled to the user by the event-handler, the user can, for whatever reason, then choose some course of action within the program other than to use the mouse to put the cursor back into the NumericUpDown control and proceed to then enter valid input. (For that matter, some computer-illiterate users may not even know HOW to use the mouse to put the cursor back into the control.)

Therefore, I want the cursor, and the flow of control, to stay within the event-handler until valid input is entered (or the program terminated), rather than to allow the flow of control to exit the NumericUpDown control and proceed to the event-handler. Thatís what Iím trying to do.

#10 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:24 PM

Posted 05 March 2009 - 09:03 PM

Then you are going to have to extend that class to do what you want. Inheritance is a beautiful thing. :thumbsup:

As far as my code sample, I hard coded the values, which is a horrible practice. I apologize. The correct way to to do it would be like this:
numericUpDown1.Value > numericUpDown1.Maximum || numericUpDown1.Value < numericUpDown1.Minimum

Then no matter the range, the code would never have to be changed.

By definition, a simple (non-compound) comparison, such as comparing the user's input to an allowed maximum, or to an allowed minimum, should involve a comparison to only ONE value.

Really? Out of curiosity, how would you propose to check against an upper and lower bounds by only comparing one value? I absolutely do not know everything there is to know about programming, so if you could show me how to do that, I certainly would be grateful. As I see it, you either have one check for the upper bounds, and one check for the lower bounds, which is equally unwieldy.

Perhaps this will help (as an example of using inheritance):
http://www.musicalnerdery.com/nerdery/fixi...wn-control.html

#11 BobLewiston

BobLewiston
  • Topic Starter

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:11:24 PM

Posted 05 March 2009 - 10:19 PM

...you are going to have to extend that class...

Yeah, that's the consensus I'm getting from the forums. I had hoped to avoid that, but oh well...

The correct way to to do it would be like this:

numericUpDown1.Value > numericUpDown1.Maximum || numericUpDown1.Value < numericUpDown1.Minimum

Right, that's what I did.

By definition, a simple (non-compound) comparison, such as comparing the user's input to an allowed maximum, or to an allowed minimum, should involve a comparison to only ONE value.

...how would you propose to check against an upper and lower bounds by only comparing one value?

By means of a compound comparison; that is, by means of the union of two simple comparisons, one for each bound, using the && or || operators.

Perhaps this will help:http://www.musicalnerdery.com/nerdery/fixi...wn-control.html

Thanks, I'll check it out.

#12 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:24 PM

Posted 06 March 2009 - 01:05 AM

By means of a compound comparison; that is, by means of the union of two simple comparisons, one for each bound, using the && or || operators.

Ok, cool. How is that different than what I showed you?

Extending classes is no big deal, and is commonplace. Thus is the beauty of OOP.

#13 BobLewiston

BobLewiston
  • Topic Starter

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:11:24 PM

Posted 06 March 2009 - 10:57 AM

By means of a compound comparison; that is, by means of the union of two simple comparisons, one for each bound, using the && or || operators.

Ok, cool. How is that different than what I showed you?

It's not different. I was merely responding to:

By definition, a simple (non-compound) comparison, such as comparing the user's input to an allowed maximum, or to an allowed minimum, should involve a comparison to only ONE value.

Really? Out of curiosity, how would you propose to check against an upper and lower bounds by only comparing one value?

Extending classes is no big deal, and is commonplace.

Yeah, I've done it, but as a .Net/C#/OOP newbie trying to get back into the programming biz after years away, I've kinda got my hands full learning a miilion things.

#14 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:24 PM

Posted 06 March 2009 - 11:01 AM

Yeah, I've done it, but as a .Net/C#/OOP newbie trying to get back into the programming biz after years away, I've kinda got my hands full learning a miilion things.

:thumbsup:
I always feel like I am trying to learn a million things. I'm getting the gray hair to prove it.

#15 BobLewiston

BobLewiston
  • Topic Starter

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:11:24 PM

Posted 06 March 2009 - 12:13 PM

Perhaps this will help (as an example of using inheritance):
http://www.musicalnerdery.com/nerdery/fixi...wn-control.html

I'm confused. I don't see any Text property in the NumericUpDown control.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users