2

Possible Duplicate:
Why am I getting a FormatException was unhandled error?

I am new to C# and Stack Overflow, so if this question is inappropriate (or in the wrong place) feel free to edit or remove it.

I've made a simple calculator, but I have a problem: when I clear one of the textboxes to enter another number a message shows saying "Unhandled exception occured" with the option to quit or continue. How can I stop this message from displaying every time I clear the texboxes?

private void button1_Click(object sender, EventArgs e)
{
    int value1 = Convert.ToInt32(textBox1.Text);
    int value2 = Convert.ToInt32(textBox2.Text);
    textBox3.Text = sum(value1, value2).ToString();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    int vlera1 = Convert.ToInt32(textBox1.Text);
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
    int vlera2 = Convert.ToInt32(textBox2.Text);
}

private void textBox3_TextChanged(object sender, EventArgs e)
{ }

int sum(int value1, int value2) {
    return (value1) + (value2);
}
Community
  • 1
  • 1
hekri
  • 307
  • 3
  • 11
  • why you use vlera1 and vlera2?(why do you need them?) – Sirwan Afifi Nov 10 '12 at 11:59
  • @JonSkeet I saw that but I can't relate it to my problem. :( . – hekri Nov 10 '12 at 12:01
  • @hekri: Why not? It's exactly the same problem: whenever the text is changing, you're unconditionally calling `Convert.ToInt32`, which is like calling `int.Parse`. The exact same solutions apply. (It's not clear why you're performing conversions in methods and then discarding the results anyway, but...) – Jon Skeet Nov 10 '12 at 12:03
  • @SirwanAfifi I think I'm getting what the user inputs in the texbox and asigning it to a variable (vlera1). Like I said I'm new to this thanks for your replies. – hekri Nov 10 '12 at 12:04

2 Answers2

3

Use int.TryParse(string s, out int result) instead of Convert.ToInt32(string value, int fromBase)
this is happening because you are trying to convert the empty data of the TextBox to Int32.

if (int.TryParse(textBox1.Text, out vlera1))
{
    //assign here    
}
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • Thank you, Ravidra Bagale but when I what you said an error shows up, "cannot implicitly convert type 'bool' to 'int'". – hekri Nov 10 '12 at 12:16
1

You'll receive a FormatException when you try to convert a string that can not be converted to a struct of type int to int. You may always use int.TryParse(string s, out int result) to see if the string is capable for int conversion before doing the conversion.

Example

private void textBox1_TextChanged(object sender, EventArgs e)
{
    int x = 0; //Initialize a new int of name x and set its value to 0
    if (int.TryParse(textBox1.Text, out x)) //Check if textBox1.Text is a valid int
    {
        int vlera1 = Convert.ToInt32(textBox1.Text); //Initialize a new int of name vlera2 and set its value to (textBox1.Text as int)
    }
    else
    {
        //DoSomething if required
    }
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
    int x = 0; //Initialize a new int of name x and set its value to 0
    if (int.TryParse(textBox2.Text, out x)) //Check if textBox2.Text is a valid int
    {
        int vlera2 = Convert.ToInt32(textBox2.Text);  //Initialize a new int of name vlera2 and set its value to (textBox1.Text as int)
    }
    else
    {
        //DoSomething if required
    }
}

Another Solution

You may always use a try-catch statement to see if an exception was thrown from the code you provide and do something if required

Example

private void textBox1_TextChanged(object sender, EventArgs e)
{
    try
    {
        int vlera1 = Convert.ToInt32(textBox1.Text); //Initialize a new int of name vlera2 and set its value to (textBox1.Text as int)
    }
    catch (Exception EX)
    {
        MessageBox.Show(EX.Message); //(not required) Show the message from the exception in a MessageBox
    }
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
    try
    {
        int vlera2 = Convert.ToInt32(textBox2.Text);  //Initialize a new int of name vlera2 and set its value to (textBox1.Text as int)
    }
    catch (Exception EX)
    {
        MessageBox.Show(EX.Message); //(not required) Show the message from the exception in a MessageBox
    }
}

Notice: The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions

Thanks,
I hope you find this helpful :)

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37