Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keeping a panel with a fixed size 1

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
I have a panel in a form that is squared. When i maximized my form, i want the panel to stay square. The panel does not fill up the form. Only the right side. I have a DBgrid on the left. So the form has a dbgrid on the left and a square panel on the right (set al alclient).
When i maximize, i would like the panel to maximize too but stay square. I guess the panel could be floating in the middle right of the form, or the form could be constrained to a certain size.
Hope i am making sense.
Thanks for any input.
P
 
If you want the panel to stay square, then really you need to make sure that your form stays proportional (length vs. width). There is a component that can accomplish that here:


look for TNFormSizing.

hopefully, this will accomplish what you're looking for.
 
Thanks, that may work. Some really cool stuff there. Would you know if there's a component like DBlookupcombobox where you can have the text centered? I found a way for a regular combobox. I am looking for something free :)
 
No, unfortunately haven't seen one.

You can always try to code your own though.
 
First of all, you can't keep an aligned panel square...

You need to put another panel inside the aligned panel, and put all your controls inside this one. This will make it easier to calculate the positioning. Then, you need to identify whether the width or height of the main panel is smaller. Then you make the width and height of your square panel to the minimum available size.


This example is best to copy/paste these into new text files and then open the project that way, rather than fighting Delphi's interface...


Project1.dpr:
Code:
program Project1;
uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.


Unit1.dfm:
Code:
object Form1: TForm1
  Left = 392
  Top = 473
  Width = 686
  Height = 404
  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 DBGrid1: TDBGrid
    Left = 0
    Top = 0
    Width = 225
    Height = 366
    Align = alLeft
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object Panel1: TPanel
    Left = 232
    Top = 8
    Width = 273
    Height = 257
    BevelOuter = bvNone
    TabOrder = 1
    OnResize = Panel1Resize
    object Panel2: TPanel
      Left = 48
      Top = 32
      Width = 121
      Height = 113
      TabOrder = 0
    end
  end
end


Unit1.pas:
Code:
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Grids, DBGrids;
const
  MarginWidth = 5;
type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    Panel2: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure Panel1Resize(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
  Panel1.Align:= alClient;
  Panel2.Top:= MarginWidth;
  Panel2.Left:= MarginWidth;
end;
procedure TForm1.Panel1Resize(Sender: TObject);
var
  Sz: Integer;
begin
  if Panel1.Width < Panel1.Height then
    Sz:= Panel1.Width
  else
    Sz:= Panel1.Height;
  Panel2.Width:= Sz - (MarginWidth * 2);
  Panel2.Height:= Sz - (MarginWidth * 2);
end;
end.


JD Solutions
 
That example also presumes that you may be resizing the DBGrid, so it recalculates according to the main aligned panel.


JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top