Search code examples
formslazarusfreepascal

Lazarus Form resize by code


On Arch Linux, using Lazarus 1.6.4, I am trying to hold the form's height constant.

The code below is a minimal example:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormDblClick(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  h : Integer;
implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormDblClick(Sender: TObject);
begin
  ShowMessage(h.ToString);
end;

procedure TForm1.FormResize(Sender: TObject);
begin
   h := Form1.Height;
   Form1.Height:=200; //please note this line
end;

end.   

If i comment the marked line out, I can see the change in forms height by double clicking it.

However, if the Marked line is there, then the form still changes it's height, but doesn't come back to 200. Double clicking the from shows the form height is changed to 200. But that change isn't reflect in the form's actual height.

What am I doing wrong?


Solution

  • Doing things in formresize get ticklish and you are changing something that will probably fire the resize event itself which could be, well, ticklish??

    Anyway, just set minheight and maxheight to 200 and that should do the trick.