Solved

center a form within another form

Posted on 1998-08-18
5
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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