Rebelss Posted March 26, 2019 Share Posted March 26, 2019 Hello eveyone, i like to ask how can i send messages from a class to a UniForm, i have all the business logic in the TCustomer class, which have CustomerStateChange method and the class TCustomerView which is the view. How can the class TCustomer can send a message to the class TCustomerView on StateChange of dsCustomer ( linked to FDQCustomer) ? On a traditionally windows 32 application a would use SendMessage with the TCustomerView handle, to send messages to the view. Here is my TCustomer class code : unit uTCustomer; interface uses system.classes, Data.DB, FireDAC.Comp.Client, Winapi.Messages; const WM_StateChange = WM_APP + 400; WM_AfterScroll = WM_APP + 401; type TCustomer = class(TComponent) private conn: TFDConnection; FDQCustomer: TFDQuery; procedure CustomerAfterScroll(DataSet: TDataSet); procedure CustomerStateChange(Sender: TObject); public dsCustomer: TDataSource; constructor Create(AOwner: TComponent); override; end; implementation uses Winapi.Windows; { TCustomer } procedure TCustomer.CustomerAfterScroll(DataSet: TDataSet); begin SendMessage(CustomerViewHWND, WM_AfterScroll, 0, 0); end; procedure TCustomer.CustomerStateChange(Sender: TObject); begin SendMessage(CustomerViewHWND, WM_StateChange, 0, 0); end; constructor TCustomer.Create(AOwner: TComponent); begin inherited; conn := TFDConnection.Create(self); FDQCustomer := TFDQuery.Create(self); FDQCustomer.Connection := conn; dsCustomer := TDataSource.Create(self); dsCustomer.DataSet := FDQCustomer; dsCustomer.OnStateChange := CustomerStateChange; FDQCustomer.AfterScroll := CustomerAfterScroll; FDQCustomer.Open('select * from customers'); end; end. And here is the TCustomerView code : unit uTCustomerView; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIForm, uTCustomer, uniGUIBaseClasses, uniEdit; type TCustomerView = class(TUniForm) edt_idCustomer: TUniEdit; edt_NameCustomer: TUniEdit; procedure UniFormCreate(Sender: TObject); private Customer: TCustomer; { Private declarations } public { Public declarations } protected procedure CustomerStateChange(var msg: TMessage); message WM_StateChange; end; function CustomerView: TCustomerView; implementation {$R *.dfm} uses MainModule, uniGUIApplication, Data.DB; function CustomerView: TCustomerView; begin Result := TCustomerView(UniMainModule.GetFormInstance(TCustomerView)); end; { TCustomerView } procedure TCustomerView.CustomerStateChange(var msg: TMessage); begin edt_idCustomer.enabled := Customer.dsCustomer.DataSet.State in [dsInsert]; edt_NameCustomer.enabled := Customer.dsCustomer.DataSet.State in [dsInsert]; end; procedure TCustomerView.UniFormCreate(Sender: TObject); begin Customer := TCustomer.Create(self); end; end. Is this the best way to communicate through the classes ? SendMessages ? the Unigui server handle it well ? Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now