Board index » delphi » using events on components without a Form

using events on components without a Form

Hello all,

I have a code like this:

type TAdress= class
       qry: TQuery;
       procedure doSomething;
     end;

var Adress: TAdress;

...

procedure TAdress.doSomething;
begin
  qry:= TQuery.Create(nil);
  with qry do begin
    ...

   qry.Free;
  end;
end;

This code is used only in a unit without having a form the Query to put
on. So I can't doubleclick in the objectInspector and connect the event.
So how can I use the events off qry'?

Thanks

Volker

 

Re:using events on components without a Form


You should do something like this:

type
  TAddress = class
    qry: TQuery;
    procedure SayHi(DataSet: TDataSet);
    procedure DoSomething;
  end;

procedure TAddress.SayHi(DataSet: TDataSet);
begin
  ShowMessage('Hi!");
end;

procedure TAddress.DoSomething;
begin
  qry := TQuery.Create(nil);
  try   { This is just the "right" thing to do. }
    qry.SQL.Add('SELCT * FROm Somewhere');
    ...
    qry.BeforeEdit := SayHi;  // This is how you plug into events.
  finally
    qry.Free;
  end;
end;

You must make sure that the procedure should take the same parameters the
documentation specifies. Check the help on the event. Don't worry about the
"of object" part - that means it should be a procedure in a class, and not
just any procedure.

May the code be with you.

Marcelo Lopez Ruiz

Other Threads