Under FMX, TComponent.Visible effects also the design time!

In FMX, unlike the VCL, the Visible property affects both design time and runtime when set in the Object Inspector. That’s why setting Visible:= False in FMX hides components in the Form Designer, which is a big inconvenience. If you edit the form at design time often, you can keep Visible:= True in the designer and set the needed components to false in code, at program start up.

Another solution is be to define your own components.

One idea might be to override the ReadDesignVisible method of TControl.

The other idea (that I implemented) is to introduce a new property called VisibleAtRuntime:

TYPE

TLightPanel = class(TPanel)

private

FVisibleAtRuntime: Boolean;

procedure SetVisibleAtRuntime(const Value: Boolean);

protected

procedure Loaded; override;

procedure DefineProperties(Filer: TFiler); override;

procedure ReadVisibleAtRuntime(Reader: TReader); // Custom streaming method

public

constructor Create(AOwner: TComponent); override;

published

property VisibleAtRuntime: Boolean read FVisibleAtRuntime write SetVisibleAtRuntime default True;

end;

 

And here is the implementation:

constructor TLightPanel.Create(AOwner: TComponent);

begin

inherited Create(AOwner);

FVisibleAtRuntime := True; // Default: visible at runtime

// Ensure visibility in designer

if csDesigning in ComponentState

then Visible := True;

end;

 

procedure TLightPanel.SetVisibleAtRuntime(const Value: Boolean);

begin

if FVisibleAtRuntime <> Value

then FVisibleAtRuntime := Value; // No immediate visibility change here; handled in Loaded

end;

 

procedure TLightPanel.ReadVisibleAtRuntime(Reader: TReader);

begin

FVisibleAtRuntime := Reader.ReadBoolean;

end;

 

procedure TLightPanel.DefineProperties(Filer: TFiler);

begin

inherited;

Filer.DefineProperty(‘VisibleAtRuntime’, ReadVisibleAtRuntime, nil, not FVisibleAtRuntime);

end;

 

procedure TLightPanel.Loaded;

begin

inherited;

if not (csDesigning in ComponentState)

then Visible := FVisibleAtRuntime; // Set visibility based on VisibleAtRuntime at runtime

end;

 

Find the components here: https://github.com/GabrielOnDelphi/Delphi-LightSaber/tree/main/FrameFMX

 

You will find the full source code and the corresponding demo program in my LightSaber library on GitHub.

Leave a Comment

Scroll to Top