Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Dynamically keep controls centered (relative position) on an MS Access form?

Tags:

vba

ms-access

I'm working in Access 2013 and have a number of controls (listboxes, buttons, etc.) that I want to keep centered, as a group, on a form when the form is resized.

Anchoring won't accomplish what I'm looking for because I don't want to lock the controls to the top/bottom/left/right. I want them to stay in the center.

Simply using code like this me.mycontrol.left = myform.Width/2 on the form's resize event doesn't do what I'm looking for because it aligns the individual controls to the center. I want the group of controls to be centered.

Is there a way to do this?

EDIT: Here's an example, which may make this clearer. Suppose I have a 100 x 100 form with two buttons on it. The buttons are 20 units tall and are spaced 10 units apart. They'd have the following positions:

Button1.Top = 25 (10 unit space starts at 45) Button2.Top = 55

If the form is resized to 200 x 200, the controls would have the following positions:

Button1.Top = 75 (10 unit space starts at 95) Button2.top = 105

Ideally, I'd love to turn this into a module, where I just pass it a form and it takes the original position of each control and calculates the new position.

Edit 2:

Here's one failed attempt at it, using my real code, based onthe idea from Krish:

Private Sub Form_Resize()

Dim resizeFactor As Double

resizeFactor = Me.WindowWidth / Me.Width

Me.lstModule.Left = Me.lstModule.Left * resizeFactor
Me.ctlSubform.Left = Me.ctlSubform.Left * resizeFactor
Me.Box6.Left = Me.Box6.Left * resizeFactor

End Sub
like image 343
DataWriter Avatar asked Jan 30 '15 16:01

DataWriter


2 Answers

I think the anchoring actually is the answer. You simply create a layout grid around your controls and set anchoring like this:

_____________________|___stretch down____|___________________
stretch across top___|___your controls___|stretch across top
_____________________|___stretch down____|___________________

This way your controls will stay always in the middle of the form/subform.

EDIT: Screenshots

Layout viewForm view

EDIT: Added info about borders

Adding borders can be quite a pain, but to some extent, it is possible. You can do this by setting gridline colors, setting the gridline style to solid to your buttons (default is transparent) and adding some padding. In the following example, I set the first button gridline style to solid for LEFT, RIGHT and TOP and set padding 0.1" for those sides as well. If you continue with a similar fashion, your result will look like this:

Design viewForm view

like image 59
Marek Stejskal Avatar answered Oct 21 '22 23:10

Marek Stejskal


I could use meta code:

  • mark all controls in group with text tag ('Tag' property)
  • iterate through all controls on form and calculate leftmost, topmost, rightmost and bottommost position for controls on form (right=left+width, etc) for all controls with a matching tag
  • this is the group 'window'
  • now iterate through form again, offsetting the controls by an X/Y offset calculated in relation to the group 'window'

Or, I suppose, some real code :-)

    Public Function GetControlsWindowSize(tag As String)
    Dim f As Form
    Dim c As Control
    Dim GrpLeft As Long
    Dim GrpRight As Long
    Dim GrpTop As Long
    Dim GrpBottom As Long

        For Each c In f.Controls
            If c.Properties.Item("tag") = tag Then
                If GrpLeft = 0 Or GrpLeft > c.Left Then GrpLeft = c.Left
                If GrpRight = 0 Or GrpRight < c.Left + c.Width Then GrpRight = c.Left + c.Width
                If GrpTop = 0 Or GrpTop > c.Top Then GrpTop = c.Top
                If GrpBottom = 0 Or GrpBottom < c.Top + c.Height Then GrpBottom = c.Top + c.Height
            End If
        Next
    End Function
like image 36
Ben McIntyre Avatar answered Oct 21 '22 23:10

Ben McIntyre