C# 计算程序 - 输入文本框

本文关键字:文本 输入 计算程序 | 更新日期: 2023-09-27 18:33:23

当我点击"计算"按钮时,"附加程序"和"安装"文本框恢复为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();
    }
}
    }

C# 计算程序 - 输入文本框

在代码中,您已经声明了这两个变量

double addpro = 0;
double install = 0;

然后,在声明这些值后,您将框的文本设置为等于它们,而无需将其值设置为 0 以外的任何值。

txtInstall.Text = install.ToString();
txtAddPro.Text = addpro.ToString();

我不太确定您要实现的目标,所以我不能建议您应该在那里拥有什么,但是这是您在文本字段中显示 0 的问题的根源。