Solved

center a form within another form

Posted on 1998-08-18
5
161 Views
Last Modified: 2010-04-04
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
Comment
Question by:Kmiller
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
Matvey earned 50 total points
ID: 1337202
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
 
LVL 7

Expert Comment

by:BlackMan
ID: 1337203
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
 
LVL 7

Expert Comment

by:BlackMan
ID: 1337204
Ok Matvey, you were the fastest! But mine works better (I guess)with MDI forms!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1337205
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1337206
Wow, you were very fast... ;)

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

Regards,
Viktor Ivanov
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now