2010. január 31., vasárnap

Inherit forms


Problem/Question/Abstract:

How to inherit forms

Answer:

Insert the following in the OnCreate event of the descendant form:

procedure TForm1.FormCreate(Sender: TObject);
var
  DummyForm: TForm0; {Ancestor form}
  f: integer;
  tempComp: TComponent;
begin
  try
    DummyForm := TForm0.Create(Application);
    for f := DummyForm.ComponentCount - 1 downto 0 do
    begin
      {See if the component exists in the descendant form}
      tempComp := FindComponent(DummyForm.Components[f].Name);
      if not Assigned(tempComp) then
      begin
        {Doesn't exist so move it}
        tempComp := DummyForm.Components[f];
        DummyForm.RemoveComponent(tempComp);
        InsertComponent(tempComp);
        if tempComp is TControl then
          with tempComp as TControl do
            if Parent = DummyForm then
              Parent := Self;
      end;
    end;
    {Override form properties here}
    {WriteComponentsResFile('unit.dfm, Self)}- - - > {Uncommenting this will update
                the dfm file for the descentant form. Closing and then opening the form unit
                will allow you to edit the inherited components visually}
  finally
    DummyForm.Free;
  end;
end;

where TForm0 is the ancestor form.

If you wish to override a number of properties for any component, cut and paste the component in the dfm file. This will allow you to visually edit it.

Nincsenek megjegyzések:

Megjegyzés küldése