how to set width and height of a form in delphi
Asked Answered
F

1

8

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.

Francklyn answered 27/6, 2011 at 11:34 Comment(2)
please make sure you always include the generic delphi tag since not so many people check the delphi-7 tag. I added it this time but for future questions please remember this - you'll get better answers that way.Shocking
ok. I will do so in future, thanksFrancklyn
S
16

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.

Shocking answered 27/6, 2011 at 11:36 Comment(2)
Instead of changing Width and Height individually, I'd use SetBounds to minimize updates.Abscissa
Setting ClientHeight and ClientWidth programmatically won't affect the form if AutoSize is set to true.Reticle

© 2022 - 2024 — McMap. All rights reserved.