2013-09-29 122 views
-2

當我點擊'計算'按鈕時,'其他程序'和'安裝'文本框恢復爲0,'賬單'出現給定客戶類型的基本價格。我的代碼如下:C#計算程序 - 輸入文本框

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

幫助* what *? –

+0

當我點擊計算「附加程序」和「安裝」時,如果輸入不同的數字,則轉到0 – MSmits23

回答

0

中的代碼,你已經聲明這兩個變量

double addpro = 0; 
double install = 0; 

那麼你已經把這些之後,您正在設置框中的文本等於他們,從來沒有它們的值設置爲任何東西,但0

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

我不是你想達到什麼不太清楚,所以我不能建議你應該有什麼就有什麼,而不是,不過那是源碼e關於在文本字段中顯示0的問題。