Link to home
Start Free TrialLog in
Avatar of regent
regent

asked on

Center a frame

How do I center a frame on a form?  My form will be set to maximized, so I need the frame (and all of its contents to be centered).  Also, a frame and all of its contents (text boxes, labels, etc.) are considered one entity, the frame, right?  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of viktornet
viktornet
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of regent
regent

ASKER

Where to a need to put this code?
Avatar of regent

ASKER

Sorry, where do I need to insert this code?
You have to put the code in the Form_Resize event procedure.

Another way of doing this is to use the 'Move' method.  This is essentially the same as the code provided by victornet, but is more preferable since it does it all in one command, thus slightly faster.

On Error Resume Next
Frame1.Move (Form1.ScaleWidth - Frame1.Width) / 2, _
            (Form1.ScaleHeight - Frame1.Height) / 2
On Error GoTo 0

With this method there is two more optional parameters, [Width] and [Height] in order to resize the frame in this instance.

Remember to put some error handling as well, since when you minimize the form, some property settings might evaluate to invalid results.
Actually no error can occur since this is a simple procedure...
Put the code in the form's resize event

Avatar of regent

ASKER

Thank you very much!
No prob ;-)
A Further note:

Use the 'ScaleWidth' and 'ScaleHeight' properties of the Form, rather than just the 'Width' and 'Height' properties, since this does not take into account the Form's caption/Title bar height and the borders since a user can set these to some absurd values if he wants. (You can retrieve all of these settings by using the GetSystemMetrics API call if you insist on using just the 'Width' and 'Height' properties.)
Yeah, that might be a point....If you want the title bar to be included then use Width/Height
otherwise if you only need the Form's client area use ScaleWidth/ScaleHeight

About using the GetSystemMetrics, I think you don't need to get much into APIs to do centering of the form....

Regards,
Viktor Ivanov
Victor,

You are right about GetSystemMetrics, I just wanted to point out that there is an alternative and even trying to be a little sarcastic there to emphasize the 'over kill' of using the API.

Regards

Wille Sevenster