Handle AfterOpen and BeforeOpen for all TMyQuery in projects..

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
eduardosic
Posts: 387
Joined: Fri 18 Nov 2005 00:26
Location: Brazil

Handle AfterOpen and BeforeOpen for all TMyQuery in projects..

Post by eduardosic » Mon 10 Dec 2007 00:41

Hi Antaeus,

I have 250 TMyQuery in My project in several forms..

i Need to Show a form Before Open and close after open in all TMyQuery.

how to intercept the events OnBeforeOpen and OnAfterOpen of all the TMy of the project?

in the TMyConnection perhaps.

some suggestion?

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Mon 10 Dec 2007 09:00

You can write event handlers for both events and assign these handlers to events of each TMyQuery object on initializing your application.
The assignation method may look like this:

Code: Select all

  procedure AssignEventHandlers(Control: TWinControl);
  var
    i: integer;
  begin
    for i := 0 to Control.ControlCount - 1 do begin
      if Control.Controls[i] is TMyQuery then begin
        // assign handlers
      end;
      if Control.Controls[i] is TWinControl then begin
        AssignEventHandlers(TWinControl(Control.Controls[i]));
      end;
    end;
  end;

eduardosic
Posts: 387
Joined: Fri 18 Nov 2005 00:26
Location: Brazil

Thank you..

Post by eduardosic » Mon 10 Dec 2007 10:27

Antaeus wrote:You can write event handlers for both events and assign these handlers to events of each TMyQuery object on initializing your application.
The assignation method may look like this:

Code: Select all

  procedure AssignEventHandlers(Control: TWinControl);
  var
    i: integer;
  begin
    for i := 0 to Control.ControlCount - 1 do begin
      if Control.Controls[i] is TMyQuery then begin
        // assign handlers
      end;
      if Control.Controls[i] is TWinControl then begin
        AssignEventHandlers(TWinControl(Control.Controls[i]));
      end;
    end;
  end;
Thank's Anteaus, i go to try..

Best Regards and a good work.

eduardosic
Posts: 387
Joined: Fri 18 Nov 2005 00:26
Location: Brazil

Re: Thank you..

Post by eduardosic » Tue 11 Dec 2007 15:17

With its suggestion I developed the following code:

Code: Select all


procedure TFrmMain.doClose(DataSet: TDataSet);
begin
   ShowWait( False ); //Close a FormDlg

end;

procedure TFrmMain.doOpen(DataSet: TDataSet);
begin
   //Open a Form Dlg
   ShowWait( True, 'Comunicando com o Servidor...' );

end;

procedure TFrmMain.AssignEventHandlers(Form: TForm);
var
  nCount:Integer;
begin
   for nCount := 0 to (Form as TForm).ComponentCount -1 do
    if (Form as TForm).Components[ nCount ] is TMyQuery then begin
     ((Form as TForm).Components[ nCount ] as TMyQuery).BeforeOpen  := doOpen;
     ((Form as TForm).Components[ nCount ] as TMyQuery).AfterOpen   := doClose;
    end;

end;


in the OnCreate Event of Each Form I call procedure

AssignEventHandlers(Self);

you suggest some modification?

Thank's,

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Wed 12 Dec 2007 09:15

This code is good, and I don't think that it should be modified.

eduardosic
Posts: 387
Joined: Fri 18 Nov 2005 00:26
Location: Brazil

Thank you..

Post by eduardosic » Wed 12 Dec 2007 10:06

Antaeus wrote:This code is good, and I don't think that it should be modified.
Thank's for good help. :D

Post Reply