// 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.Drawing; using System.Windows.Forms; using log4net; using Order.Backend; namespace Order.Frontend { public class AddCustomerForm : Form { private static readonly ILog log = LogManager.GetLogger(typeof(AddCustomerForm)); private Label namelbl; private TextBox name; private Label addrlbl; private TextBox addr; private Label phonelbl; private TextBox phone; private Label emaillbl; private TextBox email; private Button add; private Label status; public AddCustomerForm() { SuspendLayout(); Text = "Add Customer"; ClientSize = new Size(400, 450); namelbl = new Label(); namelbl.Text = "Name:"; namelbl.Location = new Point(50, 100); Controls.Add(namelbl); name = new TextBox(); name.Size = new Size(200, 25); name.Location = new Point(150, 100); Controls.Add(name); addrlbl = new Label(); addrlbl.Text = "Address:"; addrlbl.Location = new Point(50, 150); Controls.Add(addrlbl); addr = new TextBox(); addr.Size = new Size(200, 25); addr.Location = new Point(150, 150); Controls.Add(addr); phonelbl = new Label(); phonelbl.Text = "Telephone:"; phonelbl.Location = new Point(50, 200); Controls.Add(phonelbl); phone = new TextBox(); phone.Size = new Size(200, 25); phone.Location = new Point(150, 200); Controls.Add(phone); emaillbl = new Label(); emaillbl.Text = "Email:"; emaillbl.Location = new Point(50, 250); Controls.Add(emaillbl); email = new TextBox(); email.Size = new Size(200, 25); email.Location = new Point(150, 250); Controls.Add(email); add = new Button(); add.Size = new Size(100, 25); add.Text = "Add"; add.Location = new Point(50, 300); add.Click += AddCustomer; Controls.Add(add); status = new Label(); status.Text = ""; status.Size = new Size(300, 50); status.Location = new Point(50, 350); Controls.Add(status); ResumeLayout(false); } void AddCustomer(object sender, EventArgs e) { Persister p = new Persister(); Customer c = new Customer { Id = p.GetNextCustomerId(), Name = name.Text, Address = addr.Text, Phone = phone.Text, Email = email.Text }; try { p.SaveCustomer(c); log.Info("Customer " + c.Id + " saved"); status.Text = "Customer " + c.Id + " saved"; } catch (Exception ex) { log.Error("AddCustomer failed with: " + ex.Message); status.Text = "Save failed"; } } } }