-2

I am Beginner here's my code I want to subtract values from one text field to another and show the result in 3rd text field but the problem is that I got the error input string format was not in correct format like that so what should I do?

I think my code is right but I don't know what is happening

private void txt_pay2_TextChanged(object sender, EventArgs e)
{
    try
    {
        if (!string.IsNullOrEmpty(txt_pay2.Text))
        {
           txt_dues2.Text = (Convert.ToInt32(pay_dues.Text) - Convert.ToInt32(txt_pay2.Text)).ToString();

        }
        else if (String.IsNullOrEmpty(txt_pay2.Text))
        {
            MessageBox.Show("Enter A Amount Please !!");
        }
    }
    catch (Exception Ex)
    {

        MessageBox.Show(Ex.Message);
    }
}
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
  • 2
    Did you put numeric values in `pay_dues.Text` and `txt_pay2.Text`? – Koby Douek Apr 18 '17 at 06:42
  • Show en example what you put in your txt boxes. – tadej Apr 18 '17 at 06:44
  • YES i put numeric values – Muhammad Osama Apr 18 '17 at 06:56
  • _"i put numeric values"_ -- clearly not. See marked duplicate for the general case surrounding the exception your getting. It _always_ means that the string value you're trying to parse doesn't match the destination data type to which you're trying to parse (`int` in this case). If you need more help than is found already on Stack Overflow, post a new question, explain what research you've already done, what you've already done in trying to fix the problem, and what _specifically_ you still can't figure out. – Peter Duniho Apr 18 '17 at 07:03

4 Answers4

0

You need to validate the textboxes have numeric values, and trim their values in case there are spaces.

Here's a suggestion how to do that:

private void txt_pay2_TextChanged(object sender, EventArgs e)
{
    string text1 = pay_dues.Text.Trim();
    string text2 = txt_pay2.Text.Trim();

    try
    {
        if (!string.IsNullOrEmpty(text1 ))
        {
            // Validate both textboxes have numeric values:
            Regex numericRegex = new Regex(@"^-?\d*(\.\d+)?$");
            if ( (numericRegex.IsMatch(text1)) &&
                 (numericRegex.IsMatch(text2)) )
            {
                txt_dues2.Text = (Convert.ToInt32(text1) - Convert.ToInt32(text2)).ToString();
            }
            else
            {
                MessageBox.Show("Please enter only numbers !");
                return;
            }

        }
        else if (String.IsNullOrEmpty(txt_pay2.Text))
        {
            MessageBox.Show("Please enter an amount.");
        }
    }
    catch (Exception Ex)
    {

        MessageBox.Show(Ex.Message);
    }
}
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
0
try
{  
  int n;
    bool isNumeric = int.TryParse(txt_pay2.Text, out n);
    if(isNumeric)
    {
     if (!string.IsNullOrEmpty(txt_pay2.Text))
                {
                   txt_dues2.Text = (Convert.ToInt32(pay_dues.Text) - Convert.ToInt32(txt_pay2.Text)).ToString();

                }
                else if (String.IsNullOrEmpty(txt_pay2.Text))
                {
                    MessageBox.Show("Enter A Amount Please !!");
                }
    }
    else
    { 
    MessageBox.Show("Enter A  Please !!");
    }
}
 catch (Exception Ex)
        {

            MessageBox.Show(Ex.Message);
        }
Utsav
  • 16
  • 3
0

private void textBox3_TextChanged(object sender, EventArgs e) { try { if (!string.IsNullOrEmpty(textBox2.Text)) { int val1 = Convert.ToInt32(textBox1.Text); int val2=Convert.ToInt32(textBox2.Text); int result = val1 - val2; txtResult.Text = result.ToString();

            }
            else if (String.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("Enter A Amount Please !!");
            }
        }
        catch (Exception Ex)
        {

            MessageBox.Show(Ex.Message);
        }
    }
Mulwaffer
  • 22
  • 4
0

I suggest something like this:

private void txt_pay2_TextChanged(object sender, EventArgs e) {
  // Contract:
  if ((String.IsNullOrEmpty(txt_pay2.Text)) {
    MessageBox.Show("Enter A Amount Please !!");

    // My suggestion (Win Forms): when asking user to enter anything (A Amount)
    // set keyboard focus for he/she start entering the value 
    if (txt_pay2.CanFocus)
      txt_pay2.Focus();

    return;
  } 

  // Try to get values:
  int Pay_Dues;
  int Pay;

  if (!int.TryParse(pay_dues.Text, out Pay_Dues))
    return; // Bad pay_dues.Text format
  else if (!int.TryParse(txt_pay2.Text, out Pay))
    return; // Bad txt_pay2.Text format 

  // We've met contract's requirements and we've got Pay_Dues, Pay values 

  // We don't want a cascade of txt_pay2.TextChanged events:
  // txt_pay2.Text = ... triggers txt_pay2_TextChanged 
  // which in turn calls txt_pay2.Text =  ...
  txt_pay2.TextChanged -= txt_pay2_TextChanged;

  try {
    txt_pay2.Text = (Pay_Dues - Pay).ToString(); 
  }
  finally {
    txt_pay2.TextChanged += txt_pay2_TextChanged;
  }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215