首页 > 解决方案 > 从客户那里获取总成本,然后清除列表框。列表框保留以前的数据

问题描述

我从 Microsoft Access 文件中导入了数据。我已经为项目和价格显示了 2 列。如果我选择一个项目,然后单击总计,它将在单独的文本框中给出订单的总计、税款和小计。当我单击清除按钮以清除文本框和顺序时,它工作正常。当我开始第二个订单并打印总计时,它会获取前一个订单的小计、税收和总计,然后将它们添加到新订单中。

private void button1_Click(object sender, EventArgs e)
{
    double sum = 0;
    double tax = 0;
    double total = 0;
    foreach (MenuItems items in OrderList)
    {
    sum += items.price;
    }
    tax = sum * MenuItems.tax;
    total = sum + tax;
    txtTax.Text = tax.ToString("c");
    txtSub.Text = sum.ToString("c");
    txtTotal.Text = total.ToString("c");
}

此按钮也可以添加订单中的所有项目,并以货币打印它们的值。

private void button2_Click(object sender, EventArgs e)
{
    txtTotal.Clear();
    txtSub.Clear();
    txtTax.Clear();
    ListBoxOrder.Items.Clear();
}

此按钮用于清除文本框和列表框中的文本。我缺少什么来重置以前的“订单”并且也没有以前的总计、小计和税收与单独的订单相加?

编辑:

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;
using System.Data.OleDb;

namespace CafeWithDatabase
{
public partial class Form1 : Form
{
    //Generic List to hold the Cafe items
    List<MenuItems> OurCafeMenu = new List<MenuItems>();
    List<MenuItems> OrderList = new List<MenuItems>();
    List<string> cafe = new List<string>();
    MenuItems item;
    int counter = 0;

    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {

        try
        {
            //Connection String to Access Database
            string conn_string = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\\Users\\Vexum\\source\\repos\\CafeWithDatabase\\CafeWithDatabase\\CafeDatabase.accdb";
            OleDbConnection conn = new OleDbConnection(conn_string);
            //open connection
            conn.Open();
            //reader
            OleDbDataReader reader;
            //command to select all items from CafeItem table, with the connection to the database
            OleDbCommand cmd = new OleDbCommand("SELECT * from CafeItems", conn);
            //execute the reader
            reader = cmd.ExecuteReader();
            //clear the listBoxMenu for any potential existing items in the box
            ListBoxMenu.Items.Clear();
            //while loop makes the reader Read the data and add to the generic list
            while (reader.Read())
            {
                counter += 1;
                item = new MenuItems();
                item.name = reader[0].ToString();
                item.price = double.Parse(reader[1].ToString());
                OurCafeMenu.Add(item);
            }
            //foreach loop puts the menu items into the listboxmenu
            foreach (MenuItems item in OurCafeMenu)
            {
                ListBoxMenu.Items.Add(string.Format("{0} --- ${1}", item.name, item.price));
            }
        }
        catch(Exception ex)
        {
            label4.Text = ex.Message;
        }
    }
    //Button to exit the application, could also be this.Close();
    private void button3_Click(object sender, EventArgs e)
    {
        Application.Exit();
    } 

    //a button to clear the text boxes and the ListBoxOrder
    private void button2_Click(object sender, EventArgs e)
    {
        txtTotal.Clear();
        txtSub.Clear();
        txtTax.Clear();
        ListBoxOrder.Items.Clear();
    }
    //The ListBoxMenu gets the selected index, and then puts it into ListBoxOrder
    private void ListBoxMenu_SelectedIndexChanged(object sender, EventArgs e)
    {
        int curItem = ListBoxMenu.SelectedIndex;
        MenuItems temp;
        ListBoxMenu.SelectedIndex = curItem;
        ListBoxOrder.Items.Add(ListBoxMenu.SelectedItem);
        temp = OurCafeMenu.ElementAt(curItem);
        OrderList.Add(temp);
    }
    //if you double click on an item in the ListBoxOrder, it takes it out of the box
    private void ListBoxOrder_DoubleClick(object sender, EventArgs e)
    {
        string i = ListBoxOrder.SelectedItem.ToString();
        ListBoxOrder.Items.Remove(i);
    }

    //button to calculate the total of the selected items
    private void button1_Click(object sender, EventArgs e)
    {
        double sum = 0;
        double tax = 0;
        double total = 0;

        foreach (MenuItems items in OrderList)
        {
            sum += items.price;
        }
        tax = sum * MenuItems.tax;
        total = sum + tax;

        txtTax.Text = tax.ToString("c");
        txtSub.Text = sum.ToString("c");
        txtTotal.Text = total.ToString("c");
    }
}
}

很抱歉没有发布整个代码。OrderList 是一个保存项目及其价格的集合。然后它获取价格,并将它们添加到小计、总计和税收的文本框中。ListBoxOrder 是项目进入的列表框,以允许人们也看到选择的内容。

标签: c#winforms

解决方案


这里有很多缺失的代码,但让我眼前一亮的第一件事是,你正在清除一个不同的列表,而不是你的计算所基于的。

你永远不会清算OrderList。添加OrderList.Clear()到您的button2_Click.


推荐阅读