[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

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