Solved

Center a frame

Posted on 1998-09-12
10
169 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Window placement 17 75
Use Multiple Forms 4 49
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 42
Passing a Text Box name to a Sub 6 76
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

895 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

18 Experts available now in Live!

Get 1:1 Help Now