我已从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是项目进入的列表框,允许人员也看到所选内容。
答案 0 :(得分:1)
这里有很多丢失的代码,但是首先引起我注意的是,您要清除的列表与计算所基于的列表不同。
您永远不会清除OrderList
。将OrderList.Clear()
添加到您的button2_Click
。