如何在C#中向GUI添加计算?

时间:2013-10-31 22:35:18

标签: c# winforms textbox compiler-errors

这是我第一次在Visual Studio上使用带有C#的Windows窗体。我试图让我的表单有一个按钮,当你点击“计算到期金额”,它将把计算的内容放入“金额到期”字段。但是,无论何时我说“textBox3 = aOrder.AmountDue()”,它都说它无法将double转换为System.Windows.Forms.TextBox。如何正确转换?这是我的程序代码。

namespace MidTermPizzas
{
   class pizzaOrder
   {
      public int numberOfCokes
      {
         get
         {
            throw new System.NotImplementedException();
         }
         set
         {
         }
      }

      public int numberOfPizzas
      {
         get
         {
            throw new System.NotImplementedException();
         }
         set
         {
         }
      }

      public double InputOrder()
      {
         const double COKE_PRICE = 1.49;
         const double PIZZA_PRICE = 7.99;
         double inputOrder = (numberOfCokes * COKE_PRICE) + (numberOfPizzas * PIZZA_PRICE);
         return InputOrder();
      }

      public double TaxDue()
      {
         const double TAX = .073;
         double taxDue = (this.InputOrder() * TAX);
         return TaxDue();
      }

      public double GetAmountDue()
      {
         double getAmountDue = this.InputOrder() + this.TaxDue();
         return GetAmountDue();
      }

      public double GetAmountPaid()
      {
         double getAmountPaid;
         return GetAmountPaid();
      }

      public double GetChargeDue()
      {
         double getChargeDue = this.GetAmountDue() - this.GetAmountPaid();
         return GetAmountPaid();
      }
   }
}

namespace MidTermPizzas
{
   public partial class Form1 : Form
   {
      pizzaOrder aOrder = new pizzaOrder();
      DailySummary aSummary = new DailySummary();

      public Form1()
      {
         InitializeComponent();
      }

      //click File, Exit
      private void exitToolStripMenuItem_Click(object sender, EventArgs e)
      {
         MessageBox.Show("Enjoy your pizza!");
         this.Close();
      }

      //click View, All Orders Placed
      private void allOrdersToolStripMenuItem_Click(object sender, EventArgs e)
      {
         AllOrdersPlaced myForm = new AllOrdersPlaced();
         myForm.Show();
      }

      //click View, Summary of Orders Placed
      private void summaryOfOrdersToolStripMenuItem_Click(object sender, EventArgs e)
      {
         SummaryOfOrdersPlaced myForm2 = new SummaryOfOrdersPlaced();
         myForm2.Show();
      }

      //text in box to the right of "Amount Due"
      private void textBox3_TextChanged_1(object sender, EventArgs e)
      {
         textBox3 = aOrder.GetAmountDue();
      }
   }
}     

4 个答案:

答案 0 :(得分:4)

textBox3.Text = Convert.ToString(aOrder.AmountDue());

假设AmountDue()返回Double。

你有两个问题,你试图将实际的文本框对象设置为字符串而不是文本框的.Text属性,并且你没有将双精度转换为字符串。

答案 1 :(得分:1)

textBox3是对象。该对象有各种方法(做东西)和属性(保存东西),特别是textBox3.Text,你可以在框中设置文本。请记住MSDN是你的朋友。

答案 2 :(得分:1)

要避免此错误,必须为Text属性指定值Order.GetAmountDue()。此属性包含TextBox的值:

textBox3.Text = aOrder.GetAmountDue();

由于必须保持类型之间的兼容性,因此您无法为Double分配TextBox,但可以为Double分配string (在这种情况下,Text属性为string)。

也许您需要格式化值,有关详细信息,请参阅此链接: Double.ToString

答案 3 :(得分:1)

除了Textbox问题,我也认为你不应该返回公共方法本身。我 而不是

 public double TaxDue()
        {
            const double TAX = .073;
            double taxDue = (this.InputOrder() * TAX);
            return TaxDue();
        }

你应该

 public double TaxDue()
        {
            const double TAX = .073;
            double taxDue = (this.InputOrder() * TAX);
            return taxDue;
        }

第一个实现没有意义。