private void btnAddItem_Click(object sender, EventArgs e) { Person selected = GetSelectedPerson(); Payment payment = GetDefaultTransaction(selected); Product product = new Product("New product", 100, payment, new List <Person>()); Product edited = ItemEditor.Edit(product, persons); if (edited != null) { Recalculate(); RefreshItems(); RefreshPersons(); RefreshButtons(); RefreshSummary(); SaveData(); SelectPerson(selected); SelectProduct(edited); } else { product.Transaction = null; } }
internal static Product Edit(Product p, List <Person> persons) { using (ItemEditor form = new ItemEditor(p, persons)) { form.tbTransactionName.Text = p.Transaction.Name; form.tbProductName.Text = p.Name; form.nProductPrice.Value = p.Price; bool all = true; bool any = false; int tabIndex = 4; Point initial = new Point(16, 43); foreach (Person person in persons) { CheckBox cb = new CheckBox(); cb.Location = initial; cb.Text = person.Name; cb.TabIndex = tabIndex++; cb.Checked = p.Receivers.Contains(person); cb.Tag = person; cb.CheckedChanged += cb_CheckedChanged; form.gbConsumers.Controls.Add(cb); initial = new Point(initial.X, initial.Y + 23); form.gbConsumers.Height += 23; form.Height += 23; all &= cb.Checked; any |= cb.Checked; } form.cbAll.CheckedChanged -= cbAll_CheckedChanged; if (all) { form.cbAll.Checked = true; } else if (any) { form.cbAll.CheckState = CheckState.Indeterminate; } else { form.cbAll.Checked = false; } form.cbAll.CheckedChanged += cbAll_CheckedChanged; DialogResult result = form.ShowDialog(); if (result == DialogResult.OK) { p.Name = form.tbProductName.Text; p.Price = form.nProductPrice.Value; p.Receivers.Clear(); if (form.cbAll.CheckState == CheckState.Checked) { p.Receivers.AddRange(persons); } else { foreach (CheckBox cb in form.gbConsumers.Controls) { if (cb != form.cbAll && cb.CheckState == CheckState.Checked) { if (cb.Tag as Person != null) { p.Receivers.Add(cb.Tag as Person); } } } } if (form.transactions.Any(t => t.Name == form.tbTransactionName.Text)) { p.Transaction = form.transactions.First(t => t.Name == form.tbTransactionName.Text); } else { p.Transaction = new Payment(form.tbTransactionName.Text, p.Transaction.Byer); } return(p); } else { return(null); } } }