当我点击“计算”按钮时,“附加程序”和“安装”文本框将恢复为0,并且“帐单”将显示给定客户类型的基本价格。我的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Bus432_Assignment2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
const double RES_HOME = 99;
const double RES_HOME_ADD = 49;
const double ENT_ADD_PRO = 10;
const double ENT_BASIC = 249;
const double ENT_PER_PRO = 99;
const double ENT_ADD_INSTALL_CHARGE = .10;
const double UNLIM_BASIC = 999;
char customerType;
double addpro = 0;
double install = 0;
double bill = 0;
// Check to make sure type has been entered.
if (txtCustomerType.Text == "")
{
MessageBox.Show("Customer type is required.",
Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtCustomerType.Focus();
return;
}
// Get customer type
customerType = char.Parse(txtCustomerType.Text);
customerType = char.ToUpper(customerType);
txtCustomerType.Text = customerType.ToString();
// Check customer type, programs, installations
txtInstall.Text = install.ToString();
txtAddPro.Text = addpro.ToString();
if (customerType == 'H' || customerType == 'E')
{
if (txtInstall.Text == "")
{
MessageBox.Show("For home use customer plan, enter installations",
Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
if (txtInstall.Text == "")
{
txtInstall.Focus();
}
return;
}
}
else if (customerType == 'E' || customerType == 'H')
{
if (txtAddPro.Text == "")
{
MessageBox.Show("For enterprise or home use customer, enter additional programs",
Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
if (txtAddPro.Text == "")
{
txtAddPro.Focus();
}
return;
}
else if (customerType != 'E' || customerType != 'H' || customerType != 'U')
{
MessageBox.Show("Customer type must be E, H, or U.",
Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
//txtInstall.Focus();
return;
}
// Get Additional Programs and installations
addpro = double.Parse(txtAddPro.Text);
install = double.Parse(txtInstall.Text);
if (addpro < 0)
{
MessageBox.Show("Number of additional programs must not be negative.",
Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtAddPro.Focus();
return;
}
else if (install < 0 )
{
MessageBox.Show("Number of installations must not be negative.",
Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtInstall.Focus();
return;
}
}
// Calculate Bill
switch(customerType)
{
case 'H':
bill = RES_HOME + addpro * RES_HOME_ADD;
break;
case 'E':
bill = ENT_BASIC + addpro * ENT_PER_PRO *(1+ENT_ADD_INSTALL_CHARGE*(install-ENT_ADD_PRO));
break;
case 'U':
bill = UNLIM_BASIC;
break;
}
// Display the result.
txtBill.Text = bill.ToString("C");
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}
答案 0 :(得分:0)
在代码中,您已声明了这两个变量
double addpro = 0;
double install = 0;
然后,在声明了这些之后,您将框的文本设置为与它们相等,而不将其值设置为除0之外的任何值。
txtInstall.Text = install.ToString();
txtAddPro.Text = addpro.ToString();
我不太确定你想要达到的目标,所以我不能建议你应该在那里做什么,但是这是你在文本字段中显示0的问题的根源。