Re-center popup form upon changing the width and height of popup form

Hi Bruno,

A quick question here: I would like to change the width and height of popup form after it is created (below is the code I am using):

procedure TMainForm.WebButton1Click(Sender: TObject);
procedure AfterCreate(aView : TObject);
begin
PopupForm.Height := 800;
PopupForm.width := 1200;
end;
begin
PopupForm := TPopupForm.CreateNew(@AfterCreate);
PopupForm.Popup := True;
PopupForm.PopupOpacity := 0.2;
PopupForm.PopupClose := pcNever;
PopupForm.Border := fbDialogSizeable;
PopupForm.PopupClose := pcNever;
//Form1.showmodal(@AfterShowModal);

However, as you can see from below image, the popup form is not centered. Could you give me some advice about how to center popup form in runtime? thanks

Here is the demo you can play with:
36. PopupForm.zip (1.8 MB)

When I add the code in the multiform demo to set width/height after the form load, this works:

procedure TForm1.WebButton1Click(Sender: TObject);
var
  newform: TForm2;

begin
  newform := TForm2.Create(Self);
  newform.Caption := 'Child form';
  newform.Popup := WebRadioGroup1.ItemIndex <> 0;

  case WebRadioGroup1.ItemIndex of
  2: newform.Border := fbDialog;
  3: newform.Border := fbDialogSizeable;
  end;

  // used to manage Back button handling to close subform
  window.location.hash := 'subform';

  // load file HTML template + controls
  await(TForm2, newform.Load());

  // init control after loading
  newform.frm2Edit.Text := WebEdit1.Text;
  
  // programmatically change the size of the popup form here
  newform.Width := 250;
  newform.Height := 250;

  try
    // excute form and wait for close
    await(TModalResult, newform.Execute);
    ShowMessage('Form 2 closed with new value:"'+newform.frm2Edit.Text+'"');
    WebEdit1.Text := newform.frm2Edit.Text;
  finally
    newform.Free;
  end;
end;

Hi Bruno,

Thanks for getting back to me. I copied/pasted your code into the demo and set both height and width to 800. As indicated below, the initial positioning of popup form still does not properly align it at the center of the page

Here is demo version 2 updated based on your code:
36. PopupForm V2.zip (1.8 MB)

Add this code:

  PopupForm.Width := 800;
  PopupForm.Height := 800;
  PopupForm.Top := (ClientHeight - PopupForm.Height) div 2;
  PopupForm.Left := (ClientWidth - PopupForm.Width) div 2;

That works, Thanks Bruno!!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.