我需要在Visual Studio C#中帮助我的程序。用户必须能够在四个空白文本框中输入值。当用户点击“计算”时,文本框中的数字应与其设定价格相乘。换句话说,我希望四个文本框与它们的价格相乘。
表格
这是计算代码。我设法让孩子5-12文本框进行计算。
java.util.zip
表单如何计算
答案 0 :(得分:0)
在btnCalculate_Click
代码中尝试以下内容:
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);
}
Try Catch
块已更改,因为您应该只处理Try
块中的系统错误。
请注意,我做了以下假设:
int
。由于这很快,可以扩展代码以删除一些假设并添加舍入。
答案 1 :(得分:-1)
行。我打算向你解释一下:
表单中有8个文本框。 它们都有像textBox1,textBox2等标识符。
您想要将textBox1与textBox2相乘。对于其他3对,这同样如此。然后将总计加起来并将其显示在messageBox中。
我会尝试以正确的方式发送给您:
private void btnCalculate_Click (object sender, EventArgs e)
{
int FirstPrice, SecondPrice, ThirdPrice, FourthPrice;
int FirstQnty, SecondQnty, ThirdQnty, FourthQnty;
int answer = 0;
try
{
FirstPrice = (int)TextBox1.Text.Replace("£", "");
SecondPrice = (int)TextBox2.Text;
ThirdPrice = (int)TextBox3.Text.Replace("£", "");
FourthPrice = (int)TextBox4.Text;
FirstQty = (int)TextBox5.Text.Replace("£", "");
SecondQty = (int)TextBox6.Text;
ThirdQty = (int)TextBox7.Text.Replace("£", "");
FourthQty = (int)TextBox8.Text;
answer = FirstPrice * FirstQty;
answer += SecondPrice * SecondQty;
answer += ThirdPrice * ThirdQty;
answer += FourthPrice * FourthQty;
MessageBox.Show("Your total is £" + answer.ToString());
}
catch (FormatException)
{
MessageBox.Show("Please enter a decimal value");
}
}
它可能包含一些拼写错误,但它应该有效。 只需将TextBox标识符替换为表单中的标识符即可。