Search code examples
delphidelphi-7

how to set width and height of a form in delphi


How can I set the width and height of a form in Delphi 7? The form contains different types of controls on it. I need to set the main form size to 127x263. It should change programmatically in a button click.


Solution

  • Like so:

    MainForm.Width := 127;
    MainForm.Height := 263;
    

    Or perhaps you want to set the client area to those dimensions:

    MainForm.ClientWidth := 127;
    MainForm.ClientHeight := 263;
    

    Of course, you most commonly set these properties in the Object Inspector at design time and then they are written to your form's .dfm file.

    If you want such a change to occur on a button click add a handler for the button click that looks like this:

    procedure TMainForm.Button1Click(Sender: TObject);
    begin
      Width := 127;
      Height := 263;
    end;
    

    In this last excerpt you don't need to specify the MainForm object instance because the event handler is a member of the TMainForm class and so the Self is implicit.

    If you wish to follow Ulrich Gerhardt's advice (see comment) and use SetBounds then you would write:

    SetBounds(Left, Top, 127, 263);
    

    Finally, if your form has Scaled = True then you need to deal with font scaling. Hard coded pixel dimensions like this will not be appropriate for machines with font scaling set to a different value from your machine.