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.
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:
public partial class InvoiceLineEditorForm : Form {
public InvoiceLineEditorForm() {
InitializeComponent();
}
public INVOICE_LINE InvoiceLine { get; set; }
private void InvoiceLineEditorForm_Load(object sender, EventArgs e) {
if (this.InvoiceLine.PRODUCT != null) {
edtProduct.Text = this.InvoiceLine.PRODUCT.NAME;
edtPrice.Text = this.InvoiceLine.PRODUCT.PRICE.ToString("F2");
btnChooseProduct.Click -= this.btnChooseProduct_Click;
}
if (this.InvoiceLine.QUANTITY == 0)
this.InvoiceLine.QUANTITY = 1;
edtQuantity.DataBindings.Add("Value", this.InvoiceLine, "QUANTITY");
}
private void btnChooseProduct_Click(object sender, EventArgs e) {
GoodsForm goodsForm = new GoodsForm();
if (goodsForm.ShowDialog() == DialogResult.OK) {
InvoiceLine.PRODUCT_ID = goodsForm.CurrentProduct.Id;
edtProduct.Text = goodsForm.CurrentProduct.Name;
edtPrice.Text = goodsForm.CurrentProduct.Price.ToString("F2");
}
}
}