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
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
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:
I could use meta code:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With