Board index » delphi » Notify a form of changes?

Notify a form of changes?

How does one go about notifing a form of changes in a table on another
form or data module without using the name of the form.

I have multiple tables in a data module and a form with buttons,
radiobuttons, checkboxes, ect. on it. I want to be able to enable,
disable or check the buttons when changes are made to the tables.

Right now, in any OnChange or OnDataChange events, I use something like
the Following:

Form1.Button.Enable := false;

The problem is when I use the same data module with different forms in
different programs that approach doesn't work. I need to use the same
DataModule because the other programs use the same procedures, and I
don't want to be copying the changes in code back and forth between data
modules.

Thanks
Suzy...@prairie.lakes.com

 

Re:Notify a form of changes?


Make all of your forms a descendent of a form type which defines an
event property that the datamodule can call.   The datamodule only
needs to be aware of the child form type rather than each form
instance.

 --- x

Xavier Pacheco (TeamB)
Sorry but TeamB cannot answer support questions received via email.

Re:Notify a form of changes?


Quote
Xavier Pacheco (TeamB) wrote:

> Make all of your forms a descendent of a form type which defines an
> event property that the datamodule can call.   The datamodule only
> needs to be aware of the child form type rather than each form
> instance.

I'm confused now.  Are you talking about making a new component based on
TForm and using that form to make all other forms or just setting up a
form with the event property and using that?

If it is just setting a form up, how do I go about setting up an event
property.

suzy...@prairie.lakes.com

Re:Notify a form of changes?


Quote
Rick Hollerich <suzy...@prairie.lakes.com> wrote:
>Right now, in any OnChange or OnDataChange events, I use something like
>the Following:

>Form1.Button.Enable := false;

>The problem is when I use the same data module with different forms in
>different programs that approach doesn't work.

Why don't you add a procedure to your DataModule which your forms can use
to "register" themselves?

Taking it one step further, you could have the forms pass the specific
controls which need tweaking (Form1.Button in your example).

Taking it one step even further, you could pass the address of a procedure
(ForwardOnChange) and let the form module do the dirty work.

- Tim Roberts, t...@probo.com
  Providenza & Boekelheide, Inc

Re:Notify a form of changes?


I'm talking about make a descendant of TForm which contains an event
handler that can be called by the Datamodule.  This descendant could
look like:

  TBaseForm = class(TForm)
  private
     FDataModUpdate: TNotifyEvent;
  public
    property OnDataModUpdate: TNotifyEvent read FDataModUpdate write
         FDataModUpdate;
  end;

 Now, all your forms will descend from this class and will inherit the
property.

  Ok, now the Datamodule only needs to be aware of this class type and
it won't matter which form instance is causing an update to occur. So,
lets say you've created the form instance:

    TNewForm = class(TBaseForm)
    private
      procedure MyDataModUpdate(Sender: TObject);
   ...
  end;

  In it's OnCreate event handler you assign MyDataModUpdate to the
property OnDataModUpdate ie:

   TNewForm.FormCreate(...)
   begin  
       OnDataModUpdate := MyDataModUpdate;
   end;

   Of course you'll have to define MyDataModUpdate to do whatever you
want done when an update occurs.

In the datamodule procedure to perform an update,  you could either
pass this form instance as a parameter, so that the event can be
called, or you can loop through all instances of TNewForm in the app
and call there OnDataModUpdate events if they occur.

  ie:

 procedure  TMyDatamod.SaveSomeRecord( aForm: TBaseForm );
 begin
    { do update }
    if Assigned(aForm.OnDataModUpdate) then
       aForm.OnDataModUpdate(self);
 end;

That's one take on this.  

 ---- x

Xavier Pacheco (TeamB)
Sorry but TeamB cannot answer support questions received via email.

Re:Notify a form of changes?


Thanks for the help.  After some research, I came up with a slightly
different version.  Do you see any possible problems with my version?

{This is the base form}
{There is no other code in the base form}
type
  TDataSetForm = class(TForm)
  private
  public
     procedure OnDataSetChanged(DataSet : TDataSet); virtual;
  end;

var
  DataSetForm: TDataSetForm;

implementation

{$R *.DFM}

Procedure TDataSetForm.OnDataSetChanged(DataSet : TDataSet);
begin
  {I do nothing here, It is blank}
end;

{ This is in DataModule}
{It loops though all forms in          }
{application looking for TDataSetForm's}

procedure TDataM.AfterEdit(DataSet: TDataSet);
var
  A : Integer;
begin
  with Application do
    for a := 0 to componentcount-1 do
      if Components[a] is TDataSetForm then
        TDataSetForm(components[a]).OnDataSetChanged(DataSet);
end;

{ This is a form derived from TDataSetForm }

type
  TClientForm = class(TDataSetForm);
 Public
   procedure OnDataSetChanged(DataSet : TDataSet); Override;
end;

implementation

procedure TClientForm.OnDataSetChanged(DataSet: TDataSet);
begin
  {processes the updates I need in the form}
end;

end;

Right now I autocreate all my forms.  Will this approach work if I
create the forms as needed?

Thanks for all your help and advice. I greatly appreciate it.
suzy...@prairie.com

Other Threads