How can I align my panel according to my button ?

Hello guys,

as you can see in the picture I have a small panel with some box inside of it showing a legend of my color. But this panel only will be displayed if the user clicks on the last button. My need a way to show
this panel align that way, to the left side according to my buttom position. How can I do that through
rect or clientrect?

Did you get it?
Thanks
img1.jpg
LVL 1
hidrauAsked:
Who is Participating?
 
diniludCommented:
check my example


dfm
=====

object Form1: TForm1
  Left = 115
  Top = 114
  Width = 544
  Height = 375
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Panel2: TPanel
    Left = 48
    Top = 8
    Width = 353
    Height = 73
    Caption = 'Panel2'
    TabOrder = 1
    object Button1: TButton
      Left = 120
      Top = 16
      Width = 169
      Height = 41
      Caption = 'Button1'
      TabOrder = 0
    end
  end
  object Panel1: TPanel
    Left = 112
    Top = 144
    Width = 241
    Height = 57
    Caption = 'Panel1'
    TabOrder = 0
  end
end

Pas
=====

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var pt:TPoint;
begin
  pt:=Button1.Parent.ClientToScreen(Point(Button1.Left,Button1.Top));
  pt:=ScreenToClient(pt);
  MoveWindow(Panel1.Handle,pt.X+Button1.Width-Panel1.Width,pt.Y+Button1.Height,
         Panel1.Width,Panel1.Height,True);
end;

end.
0
 
2266180Commented:
when you show the panel, you will set the top, left and/or right properties of the panel to corespond to what you want.

for example:
panel.right:=button.right;
panel.top:=button.bottom+5;// leave 5 pixels between button and panel
panel.show;
0
 
diniludCommented:
Panel1.Top:=Button1.Top+Button1.Height+1;
 Panel1.Left:=Button1.Left+Button1.Width-Panel1.Width
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Geert GOracle dbaCommented:
let's say you put the panel on an other form.
The reason ?
When you hit the button, the legend may de partially off screen

this routine starts with the legend window below the button aligned on the left side
if it would be partially invisible on the right or on the bottom it is moved
procedure ShowLegend(Sender: TObject);
var R: TRect;
  Offsets, Pt: TPoint;
  frm: TForm;
  btn: TButton;
begin
  frm := TForm(TComponent(Sender).Owner);
  btn := TButton(Sender);
  frmLegend := TfrmLegend.Create(frm);
  try
    R := Rect(0,0,frmLegend.Width,frmLegend.Height);
    Pt := frm.ClientToScreen(Point(0,0));
    OffsetRect(R, Pt.X, Pt.Y); // Move to Top left client of form
    OffsetRect(R, btn.Left, btn.Top); // Move to top left of button
    OffsetRect(R, 0, btn.Height); // Move below button
    if R.Right > Screen.Width then // Move to left if off screen on right
      OffsetRect(R, btn.Width - frmLegend.Width, 0);
    if R.Bottom > Screen.Height then // Move up if below screen
      OffsetRect(R, 0, 0 - btn.Height - frmLegend.Height);
    frmLegend.Top := R.Top;
    frmLegend.Left := R.Left;
    frmLegend.ShowModal;
  finally
    FreeAndNil(frmLegend);
  end;
end;

Open in new window

0
 
hidrauAuthor Commented:
this work fine if my button is on the form

  suiPanel1.Left  := SpeedButton5.Left - suiPanel1.Width ;
  suiPanel1.Top   := SpeedButton5.Top  - suiPanel1.Height;
  suiPanel1.Show;

but this doesn't work if my speedbutton5 is in the other panel in the center of my form
0
 
hidrauAuthor Commented:
I didn't understand you example Geert_Gruwez to adapt it to my needs
0
 
diniludCommented:
anyway i am leaving.
0
 
hidrauAuthor Commented:
Thanks very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.