// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; using log4net; using Order.Backend; namespace Order.Frontend { internal class PQ { public int P { get; set; } public int Q { get; set; } } public class AddOrderForm : Form { private const string DEFAULT_STATUS = "Open"; private static readonly ILog log = LogManager.GetLogger(typeof(AddOrderForm)); private Label custlbl; private ComboBox cust; private Button add; private Label status; private DataGridView lines; private Label prodlbl; private ComboBox prod; private Label qlbl; private TextBox q; private Button addline; private IList templines; public AddOrderForm() { Persister p = new Persister(); templines = new List(); SuspendLayout(); ClientSize = new Size(500, 550); Text = "Add Order"; custlbl = new Label(); custlbl.Text = "Customer:"; custlbl.Location = new Point(50, 50); Controls.Add(custlbl); cust = new ComboBox(); cust.DataSource = p.GetCustomers(); cust.DisplayMember = "Name"; cust.ValueMember = "Id"; cust.Location = new Point(150, 50); Controls.Add(cust); add = new Button(); add.Text = "Add"; add.Location = new Point(350, 50); add.Click += AddOrder; Controls.Add(add); status = new Label(); status.Text = ""; status.Size = new Size(300, 50); status.Location = new Point(50, 100); Controls.Add(status); lines = new DataGridView(); lines.Size = new Size(400, 150); lines.Location = new Point(50, 150); lines.Columns.Add(null, null); lines.Columns.Add(null, null); lines.Columns.Add(null, null); lines.Columns.Add(null, null); lines.Rows.Clear(); lines.ReadOnly = true; lines.ScrollBars = ScrollBars.None; lines.ColumnHeadersVisible = false; lines.RowHeadersVisible = false; Controls.Add(lines); prodlbl = new Label(); prodlbl.Text = "Product:"; prodlbl.Location = new Point(50, 350); Controls.Add(prodlbl); prod = new ComboBox(); prod.DataSource = p.GetProducts(); prod.DisplayMember = "Name"; prod.Location = new Point(150, 350); Controls.Add(prod); qlbl = new Label(); qlbl.Text = "Quantity:"; qlbl.Location = new Point(50, 400); Controls.Add(qlbl); q = new TextBox(); q.Size = new Size(75, 25); q.Location = new Point(150, 400); Controls.Add(q); addline = new Button(); addline.Text = "Add"; addline.Location = new Point(50, 450); addline.Click += AddOrderLine; Controls.Add(addline); } void AddOrder(object sender, EventArgs e) { Persister p = new Persister(); int nextoid = p.GetNextOrderId(); Order.Backend.Order o = new Order.Backend.Order { Id = nextoid, CustomerId = (int)cust.SelectedValue, OrderDate = DateTime.Now.Date, Status = DEFAULT_STATUS, Lines = new List() }; int nextolid = p.GetNextOrderLineId (); o.Lines = new List(); for(int i = 0; i < templines.Count; i++) { OrderLine ol = new OrderLine { Id = nextolid + i, OrderId = o.Id, ProductId = templines[i].P, Quantity = templines[i].Q }; o.Lines.Add(ol); } try { p.SaveOrder(o); log.Info("Order " + o.Id + " saved"); status.Text = "Order " + o.Id + " saved"; } catch (Exception ex) { log.Error("AddOrder failed with: " + ex.Message); status.Text = "Save failed"; } } void AddOrderLine(object sender, EventArgs e) { Product p2 = (Product)prod.SelectedValue; int q2; if(int.TryParse(q.Text, out q2) && q2 > 0) { templines.Add(new PQ { P = p2.Id, Q = q2 }); int ix = lines.Rows.Add(); lines.Rows[ix].Cells[0].Value = p2.Name; lines.Rows[ix].Cells[1].Value = q2; lines.Rows[ix].Cells[2].Value = p2.Price; lines.Rows[ix].Cells[3].Value = q2 * p2.Price; } else { MessageBox.Show("Invalid quantity"); } } } }