如何在MS Access窗体上动态保持控件居中(相对位置)? [英] How to Dynamically keep controls centered (relative position) on an MS Access form?

查看:505
本文介绍了如何在MS Access窗体上动态保持控件居中(相对位置)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Access 2013中工作,并具有许多控件(列表框,按钮等),它们在调整表单大小时希望作为一个组居中放置在表单上.

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.

仅在窗体的resize事件上使用像这样的代码 me.mycontrol.left = myform.Width/2 并不能满足我的要求,因为它可以将单个控件与中心对齐.我希望控件组居中.

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?

这是一个示例,可以使这一点更加清楚.假设我有一个100 x 100的表格,上面有两个按钮.这些按钮高20个单位,彼此间隔10个单位.他们将担任以下职务:

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个单位空间从45开始) Button2.Top = 55

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

如果将表单大小调整为200 x 200,则控件将具有以下位置:

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

Button1.Top = 75 (10个单位空间从95开始) Button2.top = 105

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.

根据我的真实代码,根据克里希(Krish)的想法,这是一次失败的尝试:

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.

屏幕截图

添加了有关边框的信息

添加边框可能会很痛苦,但是在某种程度上,它是可能的.您可以通过设置网格线颜色,将网格线样式设置为纯色(默认为透明)并添加一些填充来实现.在下面的示例中,我将第一个按钮网格线样式的LEFT,RIGHT和TOP设置为solid,并将这些边的padding设置为0.1.如果继续使用类似的方式,结果将如下所示:

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:

这篇关于如何在MS Access窗体上动态保持控件居中(相对位置)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆