如何在保留用户输入数据的同时清除 C# 中的表单?

发布于 2024-12-08 21:18:01 字数 146 浏览 0 评论 0原文

我正在编写一个定制饮料的程序。该程序允许用户选择尺寸、口味、配料和数量。然后,用户按下“添加饮料到订单”按钮,然后弹出一条消息“您想自定义另一种饮料以添加到您的订单中吗?是,否”我的问题是如何保存第一个自定义饮料以在订单中打印收据和第二杯饮料一起吗?清除表格时数据丢失,正确吗?

I am writing a program for customizing drinks. The program lets the user select a size, flavor, topping, and quantity. The user then presses the button "Add drink to order" and a message pops up "Do you want to customize another drink to add to your order? Yes, No" My question is how do you save the first customized drink to print in the receipt along with the second drink? When clearing the form the data is lost correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不是你的备胎 2024-12-15 21:18:01

您的程序必须保存数据。这可以放入数据库或集合中。我认为您还没有使用数据库,因此集合是最好的方法。

创建一个类,该类具有每种类型值的属性。然后列出这些内容并填充值。

Your program has to save the data. This can be into a database, or into a collection. I don't think you are using a database yet, so a collection is the best way.

Create a class that has a property for each type of value. Then make a list of those, populated with values.

岁月无声 2024-12-15 21:18:01

首先构建一个表示订单的对象

public class DrinkOrder
{
  public Object Size { get; set; }
  public Object Flavor { get; set; }
  public Object Topping { get; set; }
  public Object Quantity { get; set; }
}

注意:我使用模糊的Object类型,因为我不知道您如何表示这些字段值

然后将一个列表添加到您的 然后,当他们单击“添加”

List<DrinkOrder> Orders = new List<DrinkOrder>();

时,填充该对象并将其添加到订单列表中。

private void btnPlaceOrder_Click(object sender, EventArgs e)
{
  /* ... */

  this.Orders.Add(new DrinkOrder
  {
    Size = this.SizeControl.Value,
    Flavor = this.FlavorControl.Value,
    Toppings = this.ToppingControl.value
    Quantity = this.QuantityControl.Value
  });

  /* ... */
}

然后,您可以清除表格(如果需要)并准备好重新进行。

他们下订单后,您现在就有了一个列表 (Orders),您可以将其引用为“购物车”。

First build an object that represents the order

public class DrinkOrder
{
  public Object Size { get; set; }
  public Object Flavor { get; set; }
  public Object Topping { get; set; }
  public Object Quantity { get; set; }
}

Note: I use the vague Object type because I don't know how you're representing those field values

Then add a list to your form that retains the past/future "orders"

List<DrinkOrder> Orders = new List<DrinkOrder>();

Then, when they click add, populate the object and add it to the list of orders.

private void btnPlaceOrder_Click(object sender, EventArgs e)
{
  /* ... */

  this.Orders.Add(new DrinkOrder
  {
    Size = this.SizeControl.Value,
    Flavor = this.FlavorControl.Value,
    Toppings = this.ToppingControl.value
    Quantity = this.QuantityControl.Value
  });

  /* ... */
}

Then, you can clear the forms (if desired) and be ready to do it all over again.

Once they're done placing orders, you now have a list (Orders) you can reference as a "shopping cart".

妄想挽回 2024-12-15 21:18:01

也许你可以把数据放在某个地方?就像一个数组?

Perhaps you could put the data somewhere? Like an array?

迷鸟归林 2024-12-15 21:18:01

您可以使用数据绑定来实现此目的,它很简单并且可以保存多个数据。您可以在此处阅读更多相关信息

You can use Data Binding for this, it's easy and saves multiple data. You can read more about it here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文