Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
how to write a code for accepting above 10000 and bellow 20000 in windows textbox


can you tell me
thankss
Posted

That concept is called validation, FYI.
You check the value at your property layer (if you have one) or on the button click or where ever you want you can just write a simple if..else.. statement to check that.
Something like-
C#
try
{
  int yourValue=int.Parse(txtYourTb.Text);
  if(yourValue>10000 && yourValue<20000)
  {
     //do your stuff
  }
  else
  {
     MessageBox.Show("Invalid Input");
     return;
  }
}
catch(Exception ex)
{
   //str msg=ex.Message;
}


Hope, it helps :)
 
Share this answer
 
Personally, I wouldn't - I'd use a NumericUpDown instead.

But, if you must, you can either do it in the Validating event[^] which will occur when teh user leaves the textbox, or in the TextChanged event[^] which occurs each time it alters.

Either way, use int.TryParse[^] to convert the text to a number, check the range and report or alter it.
 
Share this answer
 
Quote:
if (Convert.ToInt32(TextBox1.Text) > 20000 || Convert.ToInt32(TextBox1.Text) < 10000)
{
MessageBox.Show("Amount b/w 10000-20000");
TextBox1.Text = "";
return;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900