how to impose numerical limits on a textbox??

W

weg22

Hi all,

I need to impose maximum and minimum numerical limits on a textbox.
I'd prefer not to code these in (since I have many textboxes and the
limits on each are different) and was wondering if there is any way to
do this within the textbox properties?

For example, I know the "NumericUpDown" has maximum and minimum
properties so that you can specify a range. This would be perfect,
however, I'm not a fan of the up/down arrows on the side. Is there a
way to bring in a NumericUpDown control and not display the up/down
arrows? If not, I'm hoping to get an answer to my question in the
first paragraph...

Thanks in advance,
-weg
 
R

rowe_newsgroups

I need to impose maximum and minimum numerical limits on a textbox.
I'd prefer not to code these in (since I have many textboxes and the
limits on each are different) and was wondering if there is any way to
do this within the textbox properties?

I don't know of a built in method, and I can't say I ever looked.
However I would recommend that you create a new class that inherits
from TextBox and add in the necessary properties and functionality to
achieve the numerical limits. Then, whenever you need the
functionality, just use the ExtendedTextBox you created instead of the
default TextBox.

Thanks,

Seth Rowe
 
Z

zacks

Hi all,

I need to impose maximum and minimum numerical limits on a textbox.
I'd prefer not to code these in (since I have many textboxes and the
limits on each are different) and was wondering if there is any way to
do this within the textbox properties?

For example, I know the "NumericUpDown" has maximum and minimum
properties so that you can specify a range. This would be perfect,
however, I'm not a fan of the up/down arrows on the side. Is there a
way to bring in a NumericUpDown control and not display the up/down
arrows? If not, I'm hoping to get an answer to my question in the
first paragraph...

Thanks in advance,
-weg

Add the necessary code the the textbox's TextChanged event handler.
 
Z

zacks

Hi all,

I need to impose maximum and minimum numerical limits on a textbox.
I'd prefer not to code these in (since I have many textboxes and the
limits on each are different) and was wondering if there is any way to
do this within the textbox properties?

For example, I know the "NumericUpDown" has maximum and minimum
properties so that you can specify a range. This would be perfect,
however, I'm not a fan of the up/down arrows on the side. Is there a
way to bring in a NumericUpDown control and not display the up/down
arrows? If not, I'm hoping to get an answer to my question in the
first paragraph...

Thanks in advance,
-weg

I know of no textbox properties that can do that. Unforch, I think you
are going to have to add some code the TextChanged event handler's for
your textboxes. In you case, you may want to write a private general
value limiter method that is invoked in the TextChanged event handlers
for those textboxes you want to limit the values in.
 
C

cfps.Christian

Hi all,

I need to impose maximum and minimum numerical limits on a textbox.
I'd prefer not to code these in (since I have many textboxes and the
limits on each are different) and was wondering if there is any way to
do this within the textbox properties?

For example, I know the "NumericUpDown" has maximum and minimum
properties so that you can specify a range. This would be perfect,
however, I'm not a fan of the up/down arrows on the side. Is there a
way to bring in a NumericUpDown control and not display the up/down
arrows? If not, I'm hoping to get an answer to my question in the
first paragraph...

Thanks in advance,
-weg

You can create your own user control and implement the functionality
of a numeric up down by putting a min/max value and validating the
entry every time. If you're not as worried about validation (or the
code is already written) you can use the LostFocus or TextChanged
events to quickly validate against whatever your min/max is and throw
a messagebox or change the value within that.
 
T

Tom Shelton

Hi all,

I need to impose maximum and minimum numerical limits on a textbox.
I'd prefer not to code these in (since I have many textboxes and the
limits on each are different) and was wondering if there is any way to
do this within the textbox properties?

A normal textbox has a MaxLength property, but nothing to cover the
minium. If you need this, then why not create a UserControl so that
you can add this behavior to a standard textbox?
 
W

weg22

Okay, that makes sense. However, I'm still new to VB. Can someone
please list some pseudocode to get me started?

Thanks,
-weg
 
C

cfps.Christian

Okay, that makes sense. However, I'm still new to VB. Can someone
please list some pseudocode to get me started?

Thanks,
-weg

I would create a class and have it inherit from textbox.
public class NumericTextBox
Inherits TextBox

Then I would define properties for the min and max value
public property MinValue as double
public property MaxValue as double

Grab a hold of the LostFocusEvent of the textbox to validate your box
public sub LostFocus ()
dim dbl as double
if Double.TryParse(me.text, dbl) then
if dbl > me.MaxValue then
me.Text = me.MaxValue.ToString()
throw exception
elseif dbl < me.MinValue then
me.Text = me.Minvalue.ToString()
throw exception
end if
else
'not a number
me.Text = me.MinValue.ToString() 'If you don't reset it you
could run into issues especially in textchanged
throw exception
end if
end sub

You can also do the above in the text changed event if you want to
validate it on every character not letting the user think they can
even input a number that big.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top