Solved

Center a frame

Posted on 1998-09-12
10
174 Views
Last Modified: 2010-04-30
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.
0
Comment
Question by:regent
[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
  • 4
  • 3
  • 3
10 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 70 total points
ID: 1434572
Frame.Left = (Form.Width / 2)-(Frame.Width / 2)
Frame.Top = (Form.Height / 2)-(Frame.Height / 2)

Regards,
Viktor Ivanov
0
 

Author Comment

by:regent
ID: 1434573
Where to a need to put this code?
0
 

Author Comment

by:regent
ID: 1434574
Sorry, where do I need to insert this code?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:wj7ster
ID: 1434575
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.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1434576
Actually no error can occur since this is a simple procedure...
Put the code in the form's resize event

0
 

Author Comment

by:regent
ID: 1434577
Thank you very much!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1434578
No prob ;-)
0
 
LVL 2

Expert Comment

by:wj7ster
ID: 1434579
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.)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1434580
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
0
 
LVL 2

Expert Comment

by:wj7ster
ID: 1434581
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

752 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