delphicomponentsalignmentanchorvcl

Problem with TWinControl component not responding to form resizing


I have a visual control written in Delphi 10.4. To make it simple to focus on the problem I created just the TwinControl with properties of Anchors and Align. I included the AutoSize property. The issue is when this component is built installed and placed on a TForm, the control will not respond to the form resizing.

The control has the control set to anchor akright. I added a TButton to the form and set its anchor to akRight. The button moves with the form stretch. My control does not. I can supply the full source of this small test. Maybe someone has seen this before. I have previously used TFrames for my components but discovered that there are several display problems with only part of the control showing or not displaying at all. My clients are not impressed. I would appreciate some wisdom.

Here is the code from the TWinControl

unit SampleControl;

    interface

    uses
      System.Classes, Vcl.Controls, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons, Vcl.Dialogs,
      Vcl.Graphics,AdvGrid,vcl.Forms,System.SysUtils,StrUtils,Messages, EBS3DataUtils,
      EBSGridSetup,System.UITypes,WinApi.Windows,vcl.Grids,AdvObj,System.Variants;

    Type
      TSampleControl = class(TWinControl)
      private
        FAnchors: TAnchors;
        FAlign : TAlign;
        FAutoSize : Boolean;
        procedure InitializeComponents;
        procedure SetAnchors(const Value: TAnchors);
        procedure SetAlign(const Value: TAlign);
        procedure SetAutoSize(const Value: Boolean);
      Public
        procedure Resize; Override;
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      Published
        property Anchors: TAnchors read FAnchors write SetAnchors default [akLeft, akTop];
        property Align: TAlign read FAlign write SetAlign default alNone;
        property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
      end;

    Procedure Register;

    implementation

    constructor TSampleControl.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      Width := 425;
      Height := 170;
      Parent := AOwner AS TWinControl;
      InitializeComponents;
    end;

    destructor TSampleControl.Destroy;
    begin
      // Free components created in InitializeComponents
      inherited Destroy;
    end;

    procedure TSampleControl.InitializeComponents;
    begin
      // Create and set up components
      FAlign := AlNone;
      FAutoSize := True;
      FAnchors := [akLeft,akTop];
    end;

    procedure TSampleControl.SetAnchors(const Value: TAnchors);
    begin
      if FAnchors <> Value then
      begin
        FAnchors := Value;
      end;
    end;

    procedure TSampleControl.SetAlign(const Value: TAlign);
    begin
      if FAlign <> Value then
      begin
        FAlign := Value;
      end;
    end;

    procedure TSampleControl.Resize;
    begin
      Inherited;
    end;

    procedure TSampleControl.SetAutoSize(const Value: Boolean);
    begin
      if FAutoSize <> Value then
      begin
        FAutoSize := Value;
      end;
    end;

    procedure Register;
    begin
      RegisterComponents('EBSH', [TSampleControl]);
    end;

    end.

Here is the code from the TForm

unit ComponentTest;

    interface

    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs,AdvUtil, Vcl.Grids, AdvObj,
      BaseGrid, AdvGrid, UniProvider, SQLServerUniProvider, MyDropdown,
      MyProgressBar, Vcl.StdCtrls, EBSGrid, JvExStdCtrls, JvMemo, Vcl.ExtCtrls,
      Notes, ContactPanel, EBSGridSetup, SampleControl;

    type
      TfrmCompTest = class(TForm)
        Button1: TButton;
        SampleControl1: TSampleControl;
        procedure FormResize(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      frmCompTest: TfrmCompTest;

    implementation

    {$R *.dfm}




    procedure TfrmCompTest.FormResize(Sender: TObject);
    begin
      SampleControl1.Resize;
    end;

    end.

Solution

  • Your control reintroduces new properties for Anchors, Align and AutoSize. These have nothing to do with the similar named properties from TControl.

    Instead of creating new properties, you should promote the original ones to make them published, and initialize them according to your needs:

    Type
      TSampleControl = class(TWinControl)
      private
        procedure InitializeComponents;
      Public
        procedure Resize; Override;
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      Published
        property Anchors default [akLeft, akTop];
        property Align default alNone;
        property AutoSize default True;
      end;
    
    procedure TSampleControl.InitializeComponents;
    begin
      // Create and set up components
      Align := alNone;
      AutoSize := True;
      Anchors := [akLeft,akTop];
    end;