enache vladd
enache vladd

Reputation: 87

How to display addition even if one of textboxes is empty?

I have this code:

private void adition()
{
    int a;
    int b;
    int c;

    if (Int32.TryParse(textbox1.Text, out a)&& Int32.TryParse(textbox2.Text, out b)&& && Int32.TryParse(textbox3.Text, out c) )
    {
        resultLabel.Text = (a + b + c).ToString();
    }

private void Result_Click(object sender, EventArgs e)
{
   adition();
}

How can I make when I press Result button to get result in resultLabel.Text even if one of textboxes is empty.

it won`t addition and display result until I fill all the fields.

Upvotes: 0

Views: 49

Answers (1)

Psi
Psi

Reputation: 6793

I guess you want to achieve something like this:

private void addition()
{

    int a = 0;
    int b = 0;
    int c = 0;

    Int32.TryParse(textbox1.Text, out a);
    Int32.TryParse(textbox2.Text, out b);
    Int32.TryParse(textbox3.Text, out c);
    resultLabel.Text = (a + b + c).ToString();
}

Not parseable fields are defaulted to 0 in this case.

Upvotes: 2

Related Questions