Solved

how to limit the input values of a textbox for input values ¿¿between 1 and 500

Posted on 2011-09-12
7
497 Views
Last Modified: 2013-12-17
how to limit the input values of a textbox for input values ¿¿between 1 and 500
0
Comment
Question by:abgconsulting
  • 4
  • 2
7 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
Comment Utility
Add code to the TextChanged event. Something like:

private void currencyTextBox_TextChanged(object sender, EventArgs e)
{
  if(Convert.ToInt32(currencyTextBox.Text) < 1 || Convert.ToInt32(currencyTextBox.Text)>500)
  {
    TextBox.Text = '0';
  }
}
0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
BTW, you might want to add a try/catch block, or some other validation, before doing the Convert.ToInt32. If there's a string in there, or some non-valid Int32 value, it will generate an error.
But that was just so you could see a way to do so.

You can also add the same validation on the submit button, or some such.
0
 

Author Comment

by:abgconsulting
Comment Utility
Thx CLuskitt
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
Glad to help :)
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
You could also use a NumericUpDown control instead of a textbox. Then you can put the max and min in the properties. Sometimes those look better for numbers and it will automatically prevent anything but digits from going into the box.
0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
I didn't suggest anything else because it depends on the environment being coded. For example, on ASP.Net you can use a MaskedEditExtender, a RangeValidator, etc.
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
Sure. I wasn't saying there was anything wrong with your solution at all. Since the zone was C# and C#.NET I assumed it's a Windows Forms Application. I should have explicitly stated my assumptions, but most .NET environments have built in ways to hande ranged integer input so I thought I'd throw that out there in case it was helpful to someone.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now