How to identify which form has changed the ownership of a component?

3

Can I identify which form has changed the ownership of a particular component?

For example, I have a Data Module with a ZTable that is accessed by several forms in the onClose events of those forms , I would need to check if my zTable was opened in form itself. If it was, I close the zTable , if it was not, I do not close.

    
asked by anonymous 05.02.2014 / 14:22

1 answer

4

The best way to do this (for me I do not know if there is an existing pattern) is to create a Helper for the zTable object.

A helper is a Delphi class helper that you can use to add more functionality to a class, without necessarily changing its structure, or making a class derived from it. That is, you will be able to continue using the component directly from the Delphi component palette.

TTableHelper = class helper for TzTable
public
  function OpenFromForm: boolean;
  function CloseFromForm: boolean;
  IsFormOpened: boolean;
  IsFormClosed: boolean;
end;

function TTableHelper.CloseFromForm: boolean;
begin
  try
    Self.Active := False;
    Result := True;
    ISFormClosed := True;
  except
    Result := False;
  end;
end;

function TTableHelper.OpenFromForm: boolean;
begin
  try
    Self.Active := True;
    Result := True;
    ISFormOpened := True;
  except
    Result := False;
  end;
end;

Do the following:

  • Create a new Unit ;
  • Place this Unit in the uses clause of each Form and Data Module to use;
  • When opening the table by the form, use table.OpenFromForm ;
  • When closing the table by the form, use table.CloseFromForm ;
  • No Data Module use:

    if table.IsFormOpened then
    begin
      table.isFormOpened := False;
      table.Close;
    end;
    
        
    06.02.2014 / 15:02