4.7.5. Showing Products for Selection

In the methods for adding and editing invoice lines we used the form. For displaying products, we will use a TextBox control.

fbdevgd30 efw 017 en

Figure 27. Product form

A click on the button next to the TextBox will open a modal form with a grid for selecting products. The same modal form created for displaying the products is used for selecting them. The click handler code for the embedded button that initiates the form is:

  1. public partial class InvoiceLineEditorForm : Form {
  2. public InvoiceLineEditorForm() {
  3. InitializeComponent();
  4. }
  5. public INVOICE_LINE InvoiceLine { get; set; }
  6. private void InvoiceLineEditorForm_Load(object sender, EventArgs e) {
  7. if (this.InvoiceLine.PRODUCT != null) {
  8. edtProduct.Text = this.InvoiceLine.PRODUCT.NAME;
  9. edtPrice.Text = this.InvoiceLine.PRODUCT.PRICE.ToString("F2");
  10. btnChooseProduct.Click -= this.btnChooseProduct_Click;
  11. }
  12. if (this.InvoiceLine.QUANTITY == 0)
  13. this.InvoiceLine.QUANTITY = 1;
  14. edtQuantity.DataBindings.Add("Value", this.InvoiceLine, "QUANTITY");
  15. }
  16. private void btnChooseProduct_Click(object sender, EventArgs e) {
  17. GoodsForm goodsForm = new GoodsForm();
  18. if (goodsForm.ShowDialog() == DialogResult.OK) {
  19. InvoiceLine.PRODUCT_ID = goodsForm.CurrentProduct.Id;
  20. edtProduct.Text = goodsForm.CurrentProduct.Name;
  21. edtPrice.Text = goodsForm.CurrentProduct.Price.ToString("F2");
  22. }
  23. }
  24. }