Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i want to compare two text box values, validate two textboxes in c# Windows Forms

ex:
fromtxt1 = 500000
Totxt2 = 600000

validate these textboxes, deos not allow totxt value smaller than Fromtxt value
Posted
Comments
Michael_Davies 18-Aug-15 2:21am    
Assuming you have made certain that only numbers allowed in the text boxes...Cast or convert the text box text to numeric and test.
sreeyush sudhakaran 18-Aug-15 2:22am    
int TotxtValue = Convert.ToInt32(Totxt2.Text);
int fromtxtValue = Convert.ToInt32(fromtxt1.Text);

if(TotxtValue < fromtxtValue )
{
//Give your Validation Error Message Here
}
Sathish km 18-Aug-15 2:55am    
private void etxtFromVoucherNo_TextChanged(object sender, EventArgs e)
{
//decimal FromVouNo = decimal.Parse(etxtFromVoucherNo.Text);
//decimal ToVouNo = decimal.Parse(etxtToVoucherNo.Text);

//if (FromVouNo <= ToVouNo)
//{
// etxtFromVoucherNo.Text = FromVouNo.ToString();
//}
//else
//{
// MessageBox.Show("From Voucher Number should be smaller than or equal to To Voucher Number", "Error");
//}
int ValueTo = Convert.ToInt32(etxtToVoucherNo.Text);
int ValueFrom = Convert.ToInt32(etxtFromVoucherNo.Text);

if (ValueFrom > ValueTo)
{
MessageBox.Show("From Voucher Number should be smaller than or equal to To Voucher Number", "Error");
}
}
Sathish km 18-Aug-15 2:55am    
getting error "Input string was not in a correct format. "
Sathish km 18-Aug-15 4:32am    
again It comes

Convert it into INT and then compare it wherever you want like OnClick of the button or OnTextChanged event etc.

Example:
C#
int ValueTo= Convert.ToInt32(Totxt2.Text);
int ValueFrom = Convert.ToInt32(fromtxt1.Text);
private void ValueFrom_TextChanged(object sender, EventArgs e)
{
   try
   {
      if(ValueTo < ValueFrom )
      {
         //Message here
      }
   }
   catch
   {
      // If there is an error
   }
}
 
Share this answer
 
v3
Comments
Sathish km 18-Aug-15 2:54am    
Input string was not in a correct format. When I entered into textbox.I have written in etxt_TextChanged() event is it correct?
Sanket Saxena 18-Aug-15 3:10am    
i created a textchange event
Seems your textbox value is null or not converting into INT.
Debug it and check the textboxes value
As suggested, convert the strings into numeric values and the compare the latters, e.g.
C#
bool validate()
{
  int f, t;
  if ( int.TryParse(fromtxt1, out f) == false || int.TryParse(Totxt2, out t) == false )
    return false;

  return (f <= t);
}
 
Share this answer
 

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