# How do you calculate multiple text boxes?

advertisements

I need help with my program in Visual Studio C#. The user must be able to enter a value into the four blank textboxes. The numbers in the textboxes should multiply with their set prices when the user clicks on Calculate. In other words, I want the four textboxes to multiply with their price.

The Form Here's the calculation code. I managed to get the Children 5-12 textbox to calculate.

``````private void btnCalculate_Click (object sender, EventArgs e)
{
int FirstTextBoxNumber;
int SecondTextBoxNumber;
int answer;

try
{
Convert.ToInt32(tbSecondNumber.Text);
FirstTextBoxNumber = int.Parse("2");
SecondTextBoxNumber = int.Parse(tbSecondNumber.Text);

answer = FirstTextBoxNumber * SecondTextBoxNumber;

MessageBox.Show("Your total is £" + answer.ToString());
}
catch (FormatException)
{
MessageBox.Show("Please enter a decimal value");
}
}
```
```

How the form calculates Try the following inside your `btnCalculate_Click` code:

``````        bool isNumeric = true;
double answer = 0;
double firstTextBoxNumber = 0;
double thirdTextBoxNumber = 0;
double fifthTextBoxNumber = 0;
double seventhTextBoxNumber = 0;

int secondTextBoxNumber = 0;
int fourthTextBoxNumber = 0;
int sixTextBoxNumber = 0;
int eightTextBoxNumber = 0;

try
{
if (String.IsNullOrWhiteSpace(tbFirstNumber.Text) || String.IsNullOrWhiteSpace(tbSecondNumber.Text) || String.IsNullOrWhiteSpace(tbThirdNumber.Text) || String.IsNullOrWhiteSpace(tbFourthNumber.Text) || String.IsNullOrWhiteSpace(tbFifthNumber.Text) || String.IsNullOrWhiteSpace(tbSixNumber.Text) || String.IsNullOrWhiteSpace(tbSeventhNumber.Text) || String.IsNullOrWhiteSpace(tbEightNumber.Text))
{
isNumeric = false;
}
else
{
//Check if "Prices" are all Doubles
if (isNumeric)
{
isNumeric = double.TryParse(tbFirstNumber.Text.Replace("£", ""), out firstTextBoxNumber);
}
if (isNumeric)
{
isNumeric = double.TryParse(tbThirdNumber.Text.Replace("£", ""), out thirdTextBoxNumber);
}
if (isNumeric)
{
isNumeric = double.TryParse(tbFifthNumber.Text.Replace("£", ""), out fifthTextBoxNumber);
}
if (isNumeric)
{
isNumeric = double.TryParse(tbSeventhNumber.Text.Replace("£", ""), out seventhTextBoxNumber);
}

//Check if "Qty" are all Integers
if (isNumeric)
{
isNumeric = int.TryParse(tbSecondNumber.Text, out secondTextBoxNumber);
}
if (isNumeric)
{
isNumeric = int.TryParse(tbFourthNumber.Text, out fourthTextBoxNumber);
}
if (isNumeric)
{
isNumeric = int.TryParse(tbSixNumber.Text, out sixTextBoxNumber);
}
if (isNumeric)
{
isNumeric = int.TryParse(tbEightNumber.Text, out eightTextBoxNumber);
}
}

if (isNumeric)
{
answer = firstTextBoxNumber * secondTextBoxNumber;
answer += thirdTextBoxNumber * fourthTextBoxNumber;
answer += fifthTextBoxNumber * sixTextBoxNumber;
answer += seventhTextBoxNumber * eightTextBoxNumber;

MessageBox.Show("Your total is £" + answer.ToString());
}
else
{
MessageBox.Show("Please enter a decimal value");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
```
```

The `Try Catch` block was changed because you should only handle system errors in `Try` blocks.

Please do note, I made the following assumptions:

• The price can change, and can be changed to include cents and fractions of cents.
• The price will only remove "£" and no other currency logos.
• The Qty will always be an `int` since you can't have 1.5 children.
• You are ok with the system error being shown in a message box to the user instead of being logged somewhere.

Since this was quick, the code could be expanded on to remove some of the assumptions and to add rounding.