Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从客户处获取总成本,然后清除列表框。Listbox保留以前的数据_C#_Winforms - Fatal编程技术网

C# 从客户处获取总成本,然后清除列表框。Listbox保留以前的数据

C# 从客户处获取总成本,然后清除列表框。Listbox保留以前的数据,c#,winforms,C#,Winforms,我已从Microsoft Access文件导入数据。我已经为项目和价格显示了两列。如果我选择一个项目,然后单击“总计”,它将在单独的文本框中给出订单的总计、税金和小计。当我点击我的清除按钮清除文本框和订单时,它工作正常。当我开始第二个订单并打印总额时,它会获取上一个订单的小计、税金和总额,然后将它们添加到新订单中 private void button1_Click(object sender, EventArgs e) { double sum = 0; double tax

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

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();
}
此按钮用于清除文本框和列表框中的文本。我缺少什么来重置以前的订单,也没有以前的合计、小计和税金加在一起以及单独的订单

编辑:


很抱歉没有发布完整的代码。OrderList是保存项目及其价格的集合。然后,它获取价格,并将其添加到文本框中进行小计、合计和税务。ListBoxOrder是项目进入的列表框,让用户也可以看到所选内容。

这里有很多代码缺失,但我看到的第一件事是,您正在清除一个不同的列表,然后才是您的计算依据


你永远不会清除订单列表。添加OrderList。清除按钮2\u单击。

我看到两个不同的列表:OrderList和ListBoxOrder.Items。啊,是的,有两个名称太相似的变量的老问题:哦,对不起。我会把全部代码都发出去。OrderList只是一个向ListBoxOrder.OK添加数据的列表。嗯,我已经编辑了这篇文章来展示我的全部代码,但我想这可以修复它。我想我认为当我清除列表框时,它将不记得以前的数据。非常感谢你的帮助,很抱歉在这么简单的事情上寻求帮助。是的,的确如此。很抱歉反应太晚。再次感谢你的帮助@Vexumy您绝对应该将此解决方案标记为答案:。
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");
    }
}
}