Solved

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

Posted on 2011-09-12
7
504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 36522437
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
ID: 36522444
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
ID: 36522523
Thx CLuskitt
0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
LVL 18

Expert Comment

by:Cluskitt
ID: 36522563
Glad to help :)
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36523529
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
ID: 36523568
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
ID: 36524629
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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