【问题标题】:How to hide button control in VBA如何在 VBA 中隐藏按钮控件
【发布时间】:2012-02-21 08:49:25
【问题描述】:

这里还有人在编程 VBA 吗?

我正在尝试让这段代码工作

Private Sub button3_click()

    'hide main buttons
    button1.Visible = False
    button2.Visible = False
    button3.Visible = False

    'show submenu buttons
    button4.Visible = True;
    button5.Visible = True;

End Sub

我想要做的基本上是我有一个包含 5 个主按钮控件的主窗体。其中2个在启动时隐藏。因此,当我单击按钮 3 时,我想隐藏前 3 个主按钮,并“取消隐藏”其他两个按钮。 尝试执行此事件时,出现错误

运行时错误 2165 - 您无法隐藏具有焦点的控件”。

以前有没有人接触过这方面的编程?我确定这是可行的。我只是不明白这里出了什么问题......

【问题讨论】:

  • 谢谢大家。您的所有建议都已尝试过 - 它们有效!如果您在编码时可以“跳出思维定势”,我应该认为有一种方法可以使这个简单的功能发挥作用。干杯。 :)

标签: ms-access button onclick vba


【解决方案1】:

在隐藏当前控件之前将焦点更改为可见控件之一

Private Sub button3_click()

    'show submenu buttons
    button4.Visible = True
    button5.Visible = True

    DoEvents          'execute any pending events, to make sure the button 4 and 5 are really visible
    button4.SetFocus  'change the focus to a now visible control
    DoEvents          'execute any pending events, to make sure that button4 really has the focus

    'now you can hide the other buttons

    'hide main buttons
    button1.Visible = False
    button2.Visible = False
    button3.Visible = False

End Sub

也许你可以跳过 DoEvents 命令,你应该试试

【讨论】:

    【解决方案2】:

    我会认为错误已经足够明确了。在运行代码之前将焦点移到您不想隐藏的某个控件上。另外,考虑Mehttp://msdn.microsoft.com/en-us/library/aa223099(v=office.11).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-04
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多