Graeme_Grant
Вот, держи... Быстрый взлом...
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace WFRestaurantBill
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
lbBeverage.Items.Add(new MenuItem { Name = $"Beverage {i}", Price = (decimal)(i * 3.33) });
lbAppetizer.Items.Add(new MenuItem { Name = $"Appetizer {i}", Price = (decimal)(i * 1.11) });
lbMainCourse.Items.Add(new MenuItem { Name = $"MainCourse {i}", Price = (decimal)(i * 5.55) });
lbDessert.Items.Add(new MenuItem { Name = $"Dessert {i}", Price = (decimal)(i * 4.44) });
}
BillItems = new BindingList<BillItem>();
lbBill.DataSource = BillItems;
lbBill.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
UpdateBillTotal();
}
private void OnSelectedMenuItemChanged(object sender, EventArgs e)
{
var selectedItem = (MenuItem)((ListBox)sender).SelectedItem;
var selectedName = selectedItem.Name;
var item = BillItems.Where(x => x.MenuItem.Name == selectedName).FirstOrDefault();
if (item == null)
{
item = new BillItem { Quantity = 1, MenuItem = selectedItem };
BillItems.Add(item);
}
else
{
item.Quantity++;
typeof(ListBox).InvokeMember("RefreshItems",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
null, lbBill, new object[] { });
}
BillTotal += item.MenuItem.Price;
UpdateBillTotal();
}
public BindingList<BillItem> BillItems { get; set; }
public decimal BillTotal { get; set; }
private void OnResetBillClick(object sender, EventArgs e)
{
BillTotal = 0;
BillItems.Clear();
UpdateBillTotal();
}
private void UpdateBillTotal()
{
tbTotal.Text = $"{BillTotal:N2}";
}
}
public class MenuItem
{
public string Name { get; set; }
public decimal Price { get; set; }
}
public class BillItem
{
public int Quantity { get; set; }
public MenuItem MenuItem { get; set; }
public decimal Total => Quantity * MenuItem.Price;
public override string ToString()
=> $"{Quantity.ToString("N0").PadLeft(3)} {MenuItem.Name.PadRight(15)}{Total.ToString("N2").PadLeft(6)}";
}
}
Чтобы увидеть, как это работает более подробно, вот автоматически сгенерированный код конструктора форм:
namespace WFRestaurantBill
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lbBeverage = new System.Windows.Forms.ListBox();
this.lbAppetizer = new System.Windows.Forms.ListBox();
this.lbMainCourse = new System.Windows.Forms.ListBox();
this.lbDessert = new System.Windows.Forms.ListBox();
this.lbBill = new System.Windows.Forms.ListBox();
this.tbTotal = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.butClear = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lbBeverage
//
this.lbBeverage.DisplayMember = "Name";
this.lbBeverage.FormattingEnabled = true;
this.lbBeverage.Location = new System.Drawing.Point(12, 12);
this.lbBeverage.Name = "lbBeverage";
this.lbBeverage.Size = new System.Drawing.Size(120, 95);
this.lbBeverage.TabIndex = 0;
this.lbBeverage.SelectedValueChanged += new System.EventHandler(this.OnSelectedMenuItemChanged);
//
// lbAppetizer
//
this.lbAppetizer.DisplayMember = "Name";
this.lbAppetizer.FormattingEnabled = true;
this.lbAppetizer.Location = new System.Drawing.Point(12, 113);
this.lbAppetizer.Name = "lbAppetizer";
this.lbAppetizer.Size = new System.Drawing.Size(120, 95);
this.lbAppetizer.TabIndex = 0;
this.lbAppetizer.SelectedValueChanged += new System.EventHandler(this.OnSelectedMenuItemChanged);
//
// lbMainCourse
//
this.lbMainCourse.DisplayMember = "Name";
this.lbMainCourse.FormattingEnabled = true;
this.lbMainCourse.Location = new System.Drawing.Point(12, 214);
this.lbMainCourse.Name = "lbMainCourse";
this.lbMainCourse.Size = new System.Drawing.Size(120, 95);
this.lbMainCourse.TabIndex = 0;
this.lbMainCourse.SelectedValueChanged += new System.EventHandler(this.OnSelectedMenuItemChanged);
//
// lbDessert
//
this.lbDessert.DisplayMember = "Name";
this.lbDessert.FormattingEnabled = true;
this.lbDessert.Location = new System.Drawing.Point(12, 315);
this.lbDessert.Name = "lbDessert";
this.lbDessert.Size = new System.Drawing.Size(120, 95);
this.lbDessert.TabIndex = 0;
this.lbDessert.SelectedValueChanged += new System.EventHandler(this.OnSelectedMenuItemChanged);
//
// lbBill
//
this.lbBill.FormattingEnabled = true;
this.lbBill.Location = new System.Drawing.Point(152, 12);
this.lbBill.Name = "lbBill";
this.lbBill.Size = new System.Drawing.Size(120, 290);
this.lbBill.TabIndex = 0;
//
// tbTotal
//
this.tbTotal.Location = new System.Drawing.Point(152, 337);
this.tbTotal.Name = "tbTotal";
this.tbTotal.ReadOnly = true;
this.tbTotal.Size = new System.Drawing.Size(120, 20);
this.tbTotal.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(149, 321);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(45, 13);
this.label1.TabIndex = 2;
this.label1.Text = "TOTAL:";
//
// butClear
//
this.butClear.Location = new System.Drawing.Point(152, 387);
this.butClear.Name = "butClear";
this.butClear.Size = new System.Drawing.Size(120, 23);
this.butClear.TabIndex = 3;
this.butClear.Text = "RESET BILL";
this.butClear.UseVisualStyleBackColor = true;
this.butClear.Click += new System.EventHandler(this.OnResetBillClick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 455);
this.Controls.Add(this.butClear);
this.Controls.Add(this.label1);
this.Controls.Add(this.tbTotal);
this.Controls.Add(this.lbBill);
this.Controls.Add(this.lbDessert);
this.Controls.Add(this.lbMainCourse);
this.Controls.Add(this.lbAppetizer);
this.Controls.Add(this.lbBeverage);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListBox lbBeverage;
private System.Windows.Forms.ListBox lbAppetizer;
private System.Windows.Forms.ListBox lbMainCourse;
private System.Windows.Forms.ListBox lbDessert;
private System.Windows.Forms.ListBox lbBill;
private System.Windows.Forms.TextBox tbTotal;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button butClear;
}
}