c# - 如何在 C# 中向我的 GUI 添加计算?

标签 c# winforms textbox compiler-errors

这是我第一次在带有 C# 的 Visual Studio 上使用 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();
      }
   }
}     

最佳答案

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

假设 AmountDue() 返回一个 Double。

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

关于c# - 如何在 C# 中向我的 GUI 添加计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19717751/

相关文章:

c# - Cosmos DB 在查询中不遵守 Json.Net CamelCaseNamingStrategy

c# - 引发异常或返回错误 - 更好的方法是什么?

.net - 中文操作系统(96 dpi)上的表单缩放问题

c# - 如何为图像列表中的多个图标使用单个位图? (不将其拆分为多个文件)

winforms - 好的 DataGridView 教程

c# - 使用 MVVM 选择并复制 WPF 文本框中的文本

c# - 在 Xamarin.Android 中向通知添加声音

javascript - 将下拉列表值从 View 传递到 Controller

c++ - 如何在 SendMessage 中使用 EM_SETSCROLLPOS 滚动到文本框的底部?

html - 如何修复文本框中的值?