Solved

Center a frame

Posted on 1998-09-12
10
168 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
  • 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

707 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