void SelectProduct(Product p) { foreach (ListViewItem item in lvBoughtItems.Items) { if (item.Tag == p) { item.Selected = true; break; } } }
private ItemEditor(Product initial, List <Person> data) { InitializeComponent(); template = initial; persons = data; transactions = persons.SelectMany(person => person.Payments).Where(t => t.Byer == initial.Transaction.Byer).Distinct().ToList(); tbTransactionName.AutoCompleteCustomSource = new AutoCompleteStringCollection(); foreach (Payment pay in transactions) { tbTransactionName.AutoCompleteCustomSource.Add(pay.Name); } }
private void btnRemoveItem_Click(object sender, EventArgs e) { foreach (ListViewItem item in lvBoughtItems.SelectedItems) { Product prod = item.Tag as Product; if (prod != null) { RemoveProduct(prod); } item.Remove(); } Recalculate(); RefreshSummary(); SaveData(); }
private void btnEditItem_Click(object sender, EventArgs e) { Product item = GetSelectedProduct(); Person selected = GetSelectedPerson(); if (item != null) { item = ItemEditor.Edit(item, persons); if (item != null) { Recalculate(); RefreshPersons(); RefreshItems(); RefreshSummary(); SaveData(); SelectPerson(selected); SelectProduct(item); } } }
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; } }
void RemoveProduct(Product product) { 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); } } }