【问题标题】:How to position the userform below the command button如何将用户窗体定位在命令按钮下方
【发布时间】:2011-12-04 09:34:21
【问题描述】:

我创建了一个命令按钮,当我单击该按钮时,会显示一个 userform1,并且用户窗体中有一个日历控件,它在屏幕中心弹出,但我希望它位于命令按钮下方.有没有办法说将 userform1 定位在按钮下方?

好吧,我已经给出了轴的大小及其在我的电脑上的工作,但是当像素发生变化时,它又在某个地方。

【问题讨论】:

    标签: vba


    【解决方案1】:

    我假设命令按钮在一个用户表单上,而日历控件在另一个用户表单上。

    您没有说明您使用的是哪个版本的 VBA。我在 Excel 2003 中对此进行了测试。我知道在 VB 2010 中提供了更好的解决方案。

    方法 1

    您需要第二张表格吗?您可以使用以下命令隐藏控件:

    ControlName.Visible = False
    

    并使其可见

    ControlName.Visible = True
    

    方法 2

    你需要一个模块中的一些公共变量:

    Public ButLeft As Integer
    Public ButHeight As Integer
    Public ButTop As Integer
    Public Form1Left As Integer
    Public Form1Top As Integer
    

    在命令按钮的点击事件中,包括:

    ' This stores information about the current position of
    ' Form 1 relative to the active window.
    With Me
      Form1Top = .Top
      Form1Left = .Left
    End With
    ' This stores information about the current position of the
    ' command button relative to the useable part of Form 1.
    With Me.CommandButton
      ButTop = .Top
      ButLeft = .Left
      ButLeft = .Height
    End With
    

    在表格 2 中包括:

    Private Sub UserForm_Activate()
    
      ' This positions Form 2 relative to the current position
      ' of the command button.  The "+50" is because the stored
      ' values do not allow for the width of Form1's border.
      ' Experiment to get values that look good to you. 
      With Me
        .Top = FormTop + But1Top + But1Height + 50
        .Left = FormLeft + But1Left + 50
      End With
    
    End Sub
    

    使用此代码,无论用户如何移动 Form 1,Form 2 都会显示在命令按钮下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多