unit GevondenProductenScherm; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.DBCGrids, Data.DB, Data.Win.ADODB, ObserverPattern, Util, Subscherm, ProductsAgent, Main, ApplicationContext; type TFormGevondenProducten = class(TFormSubscherm) ListViewProducten: TListView; procedure FormDestroy(Sender: TObject); procedure FormCreate(Sender: TObject); procedure ListViewProductenSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean); private FProductSubject: TSubject; FSubjectObserver: TSubjectObserver; FProductsAgent: TProductsAgent; procedure UpdateGui(Sender: TObject); public Constructor Create(AOwner: TComponent; Navigator: TFormMain; ApplicationContext: TApplicationContext; ProductSubject: TSubject; ProductsAgent: TProductsAgent); end; implementation {$R *.dfm} uses SelectService; constructor TFormGevondenProducten.Create(AOwner: TComponent; Navigator: TFormMain; ApplicationContext: TApplicationContext; ProductSubject: TSubject; ProductsAgent: TProductsAgent); begin inherited Create(AOwner, Navigator, ApplicationContext); self.FProductSubject := ProductSubject; self.FSubjectObserver := TSubjectObserver.Create(self); self.FSubjectObserver.OnChange := UpdateGui; self.FProductsAgent := ProductsAgent; end; procedure TFormGevondenProducten.FormCreate(Sender: TObject); begin FProductSubject.RegisterObserver(FSubjectObserver); end; procedure TFormGevondenProducten.FormDestroy(Sender: TObject); begin FProductSubject.UnregisterObserver(FSubjectObserver); end; procedure TFormGevondenProducten.ListViewProductenSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean); var ErrorMessage: string; begin if (Selected = false) or (Item = nil) or (Item.Data = nil) then exit; ApplicationContext.ProductsContext.GeselecteerdProduct := Item.Data; // ApplicationContext.ProductDetailContext.NotifyChanged(); FProductsAgent.ZoekProductDataMetProductNr(TProductInformatie(Item.Data).ProductNr, self.ApplicationContext.UserContext, self.ApplicationContext.ProductVerpakkingContext); if self.ApplicationContext.ProductVerpakkingContext.IsOK = false then begin // Opzoeking gefaald ErrorMessage := self.ApplicationContext.ProductVerpakkingContext.ErrorMessage; if self.ApplicationContext.ProductVerpakkingContext.InternalErrorMessage <> '' then begin ErrorMessage := ErrorMessage + sLineBreak + self.ApplicationContext.ProductVerpakkingContext.InternalErrorMessage; end; MessageDlg(ErrorMessage, TMsgDlgType.mtInformation, [mbOK], 0, mbCancel); self.ApplicationContext.ProductVerpakkingContext.Reset(); end; end; procedure TFormGevondenProducten.UpdateGui(Sender: TObject); var Product: TProductInformatie; begin ListViewProducten.Items.Clear(); ListViewProducten.Items.beginupdate(); for Product in ApplicationContext.ProductsContext.Producten do begin With ListViewProducten.Items.Add Do begin // Caption := IntToStr(Product.ProductNr); // Deze casting geeft geen access violation Caption := Product.Kol1; SubItems.Add(Product.Kol2); SubItems.Add(Product.Kol3); SubItems.Add(Product.Omschrijving1); SubItems.Add(Product.Omschrijving2); Data := Product; end; end; ListViewProducten.Items.EndUpdate; // Bij opnieuw inladen is de selectie verloren gegaan. self.ApplicationContext.ProductVerpakkingContext.Reset(); end; end.