Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

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

how to limit the input values of a textbox for input values ¿¿between 1 and 500
0
abgconsulting
Asked:
abgconsulting
  • 4
  • 2
1 Solution
 
CluskittCommented:
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
 
CluskittCommented:
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
 
abgconsultingAuthor Commented:
Thx CLuskitt
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
CluskittCommented:
Glad to help :)
0
 
TommySzalapskiCommented:
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
 
CluskittCommented:
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
 
TommySzalapskiCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now