在 Access 表单上将控件分组在一起

发布于 2024-08-09 14:55:52 字数 131 浏览 6 评论 0原文

我有一个 Access2003 表单,我想将多个控件组合在一起并通过 VBA 代码以编程方式更改可见性。

这可能吗?我确实知道我可以通过格式对项目进行分组 ->组,但如果我这样做,如何在代码中引用整个组?

谢谢

I have an Access2003 form where I wanted to group several controls together and change visibility programatically, though VBA code.

Is this possible? I do know that I can group items through Format -> Group, but if I do that, how do I refer the the entire group in my code?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谎言 2024-08-16 14:55:52

您可以将所有控件放置在组框控件中,然后更改组框本身的可见性。

您还可以在要分组的每个控件的标记属性中添加一个值,然后在 VBA 中循环遍历该控件并检查该值并更改那里的可见性。

将要分组的所有控件的 tag 属性设置为 groupABC 或任何您想要的值。

然后在代码中的某个地方使用它来循环表单控件并检查它。

Dim ctrl As Control
For Each ctrl In Me.Controls
    If ctrl.Tag = "groupABC" Then
        ctrl.Visible = False
    End If
Next

You could place all the controls in a group box control then change the visibility of the group box itself.

You could also add a value in the tag property of each control you want to group, then in VBA loop through the control and check for that value and change the visibility there.

Set the tag property of all the controls you want to group to something like groupABC or whatever you wish.

Then somewhere in your code use this to loop through the form controls and check for it.

Dim ctrl As Control
For Each ctrl In Me.Controls
    If ctrl.Tag = "groupABC" Then
        ctrl.Visible = False
    End If
Next
苏璃陌 2024-08-16 14:55:52

为了详细说明我对使用自定义集合的评论,您需要在表单的模块中执行类似的操作:

  Private mcolGroupABC As New Collection

  Private Sub IntializeCollections()
    Dim ctl As Control

    If mcolGroupABC.Count = 0 Then
       For Each ctl in Me.Controls
         If ctl.Tag = "GroupABC" Then
            mcolGroupABC.Add ctl, ctl.Name
         End If
       Next ctl
       Set ctl = Nothing
    End If
  End Sub

  Private Sub Form_Load()
    Call InitializeCollections
  End Sub

  Private Sub ShowControls(mcol As Collection, bolShow As Boolean)
    Dim ctl As Control

    For Each ctl In mcol
      ctl.Visible = bolShow
    Next ctl
    Set ctl = Nothing
  End Sub

要隐藏控件,您需要执行以下操作:

  Call ShowControls(mcolGroupABC, False)

并显示它们:

  Call ShowControls(mcolGroupABC, True)

这很简单,不是吗?

这是我一直在我的应用程序中使用的代码,自从大约 10 年前第一次实现它以来,我就一直在使用它,并且注意到使用自定义集合比遍历整个控件集合要好。

唯一需要注意的是,如果其中一个控件具有焦点,如果您尝试隐藏它,则会出错。这很容易解决,因为如果您隐藏一组控件,那么在执行此操作之前肯定有一个合适的位置来设置焦点。

To elaborate on my comment on using custom collections, you'd do something like this in your form's module:

  Private mcolGroupABC As New Collection

  Private Sub IntializeCollections()
    Dim ctl As Control

    If mcolGroupABC.Count = 0 Then
       For Each ctl in Me.Controls
         If ctl.Tag = "GroupABC" Then
            mcolGroupABC.Add ctl, ctl.Name
         End If
       Next ctl
       Set ctl = Nothing
    End If
  End Sub

  Private Sub Form_Load()
    Call InitializeCollections
  End Sub

  Private Sub ShowControls(mcol As Collection, bolShow As Boolean)
    Dim ctl As Control

    For Each ctl In mcol
      ctl.Visible = bolShow
    Next ctl
    Set ctl = Nothing
  End Sub

To hide the controls, you'd do this:

  Call ShowControls(mcolGroupABC, False)

And to show them:

  Call ShowControls(mcolGroupABC, True)

That's pretty simple, no?

This is the kind of code I use in my apps all the time, and I've used it ever since the first time I implemented it, about 10 years ago, and noticed that it was clearly noticeably faster to show/hide controls with the custom collection than it was with walking the entire Controls collection.

The only caveat is that if one of the controls has the focus, it will error out if you try to hide it. That's easily enough addressed, since if you're hiding a group of controls, you surely have an appropriate place to set the focus before you do so.

怪我入戏太深 2024-08-16 14:55:52

我喜欢 Joel Gauvreau 建议的标签属性。其他可能性包括选项卡控件和/或子表单。

I like the tag property suggested by Joel Gauvreau. Other possibilities include a tab control and / or subforms.

触ぅ动初心 2024-08-16 14:55:52

更有效的方法是将所有相关控件添加到选项卡控件中。您使用单个页面创建一个 Tab 控件,然后将 Tab 控件的 Style 设置为 None,并将 Back Style 设置为 Transparent.这样,Tab 控件的视觉外观就会消失,但其分组语义仍然存在。然后将控件添加到页面,可以在内部创建它们,也可以将现有控件剪切+粘贴到页面中(请记住在粘贴后重新启用事件过程)。

如果只是可见性问题,现在您只需设置 Page 控件的 Visible 属性,属于该控件的所有控件都会显示或隐藏。

但是,如果您需要对每个分组控件执行更详细的操作,则可以使用以下方法迭代它们:

  Dim ctl As control
  
  For Each ctl In page_control.Controls
     ctl.Visible = False
  Next

您还可以使用 .Tag 或 .Type 属性对这些控件执行不同的操作。

A more efficient approach is to add all related controls to a Tab Control. You create a Tab control with a single Page, and then set the Tab control Style to None and Back Style to Transparent. This way the visual appearance of a Tab control fades away, but its grouping semantics remain. Then add controls to the Page, either creating them inside or Cut+Paste existing controls into the page (remember to reenable event procedures after pasting).

If it’s just a matter of visibility, now you can just set the Visible property of the Page control and all controls that belong to it will show or hide.

However, if you need to do something more elaborate to each grouped control, you can iterate through them with:

  Dim ctl As control
  
  For Each ctl In page_control.Controls
     ctl.Visible = False
  Next

You can also use the .Tag or .Type properties to do different things to these controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文