Jump to content
dormky

BringToFront alternative

Recommended Posts

The following code shows that BringToFront does not work. Are there any alternatives I can use to put a control on top of all other controls in a window ?

 

interface

uses
  Vcl.Forms, Vcl.ComCtrls, System.Classes, Vcl.Controls, Vcl.StdCtrls, UUtils,
  Vcl.ExtCtrls;

type
  TFormMain = class(TForm)
    PageControlMain: TPageControl;
    Sheet1: TTabSheet;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);

    private
      dynLabel: TLabel;
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.Button1Click(Sender: TObject);
begin
  dynLabel.BringToFront();
end;

procedure TFormMain.FormCreate(Sender: TObject);
begin
  dynLabel := TLabel.Create(self);
  dynLabel.Parent := self;
  dynLabel.Height := 200;
  dynLabel.Width := 500;
  dynLabel.Top := 100;
  dynLabel.Left := 100;
  dynLabel.Caption := 'BAMKLJDOUZEHFOZEI';
  dynLabel.Visible := True;
  dynLabel.BringToFront();
end;

 

Share this post


Link to post

The issue is that BringToFront only works inside a group of TGraphicControls (like TLabel) and TWinControls. This is from the documentation of BringToFront:

Quote

Note that controls that wrap Windows screen objects (control classes descended from TWinControl) always "stack" above lightweight controls (control classes descended from TGraphicControl). BringToFront can force a lightweight control, such as a Shape, to stack above other lightweight controls, but wrapped controls, such as an Edit, will still stack above the Shape.

 

Share this post


Link to post
2 hours ago, dormky said:

TFormMain = class(TForm) PageControlMain: TPageControl; Sheet1: TTabSheet;

You could hide the pagecontrol to show the label parented to the form or change the label parent to control on top. I would use a Tpanel set to top align that "pushes" the Tabsheet/pagecontrol down to mimic the banners used in MS's XL when showing.         

Share this post


Link to post

 

19 hours ago, Pat Foley said:

You could hide the pagecontrol to show the label parented to the form or change the label parent to control on top. I would use a Tpanel set to top align that "pushes" the Tabsheet/pagecontrol down to mimic the banners used in MS's XL when showing.         

I don't want to change anything about other controls, I just need a control to be on top of all others.

 

21 hours ago, Uwe Raabe said:

The issue is that BringToFront only works inside a group of TGraphicControls (like TLabel) and TWinControls. This is from the documentation of BringToFront:

 

Yes, that's why I'm looking for an alternative. Do you know of any component that could work on the same group as my TPageControl here ? I suppose I could use such a component, make it invisible but keep a child control of it visible, if that's possible.

Share this post


Link to post

As @Dmitry Arefiev already mentioned: Replace the TLabel with TStaticText. The latter is a TWinControl and can be placed on top of other controls.

Share this post


Link to post

The TLabel was just for demonstration purposes, I'd like to be able to do that with all components...

  • Haha 1

Share this post


Link to post
25 minutes ago, dormky said:

The TLabel was just for demonstration purposes, I'd like to be able to do that with all components...

You cannot, it is only possible for TWinControl descendants.

  • Like 1

Share this post


Link to post

You can go extremely hacky and create a "transparent" panel - and simply put all your components on this. Since TPanel is a TWinControl descendant, .BringToFront will work properly on it.

 

You can use the source here, just change the painting slightly:

 

Procedure TDimPanel.Paint;
Begin
  // Omit the call to inherited in general. 
  
  Self.Canvas.Draw(0, 0, _bitmap, 255);  // Might need 0 instead of 255 for opacity - I don't know by heart
End;

 

Share this post


Link to post
20 hours ago, dormky said:

The TLabel was just for demonstration purposes, I'd like to be able to do that with all components...

Late information boo

  • Like 1

Share this post


Link to post
On 3/7/2024 at 2:36 AM, dormky said:
On 3/6/2024 at 7:27 AM, Pat Foley said:

You could hide the pagecontrol to show the label parented to the form or change the label parent to control on top. I would use a Tpanel set to top align that "pushes" the Tabsheet/pagecontrol down to mimic the banners used in MS's XL when showing.         

I don't want to change anything about other controls, I just need a control to be on top of all others.

 

 

Here's putting the label on a tabsheet.  This allows giving a context to incoming label.  By using tabsheets in the IDE you can group controls readily Then show only what is needed in runtime.  

 

var ts: TTabsheet;
var labelNu: Integer = 1;

procedure TForm1.AddLabelwithTabbedIndexClick(Sender: TObject);
begin
  ts:= TTabsheet.Create(Self);
  ts.PageControl := PageControl1;
  ts.Caption := LabelNu.ToString;
  var llabel := TLabel.Create(Self);  //was TStaticText
  llabel.Name := 'llabel' + LabelNu.ToString;
  lLabel.Caption := 'label&' + LabelNu.ToString;
  lLabel.Parent := ts;
  //LLabel.TabStop := True;
  ts.TabVisible := True;
  Inc(labelNu);
end;

procedure TForm1.RandomShowHideTabClick(Sender: TObject);
begin
  ts := PageControl1.Pages[Random(PageControl1.PageCount)];
  with ts do TabVisible := not tabVisible;
end;

procedure TForm1.Showllabel1Click(Sender: TObject);
begin
  var lbl := FindComponent('llabel1');
  (lbl as TControl).show;
end;

    

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×