Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

center a form within another form

I have small dialog/form that is started from the main form. I want this small form to always come up centered within the main form no matter where it is on the screen. How can I do this in Delphi 4.0?
0
Kmiller
Asked:
Kmiller
  • 2
  • 2
1 Solution
 
MatveyCommented:
I doubt there is some automatic method for it. There's nothing to it - just set the dialog form position according to the other form position:

frmDialog.Left := frmMain.Left + frmMain.Width div 2 - frmDialog.Width div 2;
frmDialog.Top := frmMain.Top + frmMain.Height div 2 - frmDialog.Height div 2;

-Then show it.

c u, Matvey
0
 
BlackManCommented:
I use something like this in the FormShow method on the dialog form (where frmMain is the main form):

  (* Center dialog on mainform *)
  PWidth := frmMain.Width;
  PHeight := frmMain.Height;
  Left := ((PWidth - Width) Div 2) + frmMain.ClientOrigin.X;
  Top := ((PHeight - Height) Div 2) + frmMain.ClientOrigin.Y;

0
 
BlackManCommented:
Ok Matvey, you were the fastest! But mine works better (I guess)with MDI forms!
0
 
viktornetCommented:
In the main form that sits behind not the dialog form, enter this in OnShow...

procedure MainForm.OnShow......
begin
  DialogForm.Top := ( MainForm.Height div 2 ) - ( DialogForm.Height div 2 );
  DialogForm.left := ( MainForm.Width div 2 ) - ( DialogForm.Width div 2 );
end;

That's all you need to do! ;)

Regards,
Viktor Ivanov
0
 
viktornetCommented:
Wow, you were very fast... ;)

Matvey did you see the comp I sent you the other day? Is it cool?

Regards,
Viktor Ivanov
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now