简体   繁体   English

VBA UserForm可见范围

[英]VBA UserForm visible range

I have a userform with the dimensions (h*w) 180 * 240. To this userform controls are to be added by the UserForm_Initialize() event based on a myriad of user inputs after which the userform is to be resized . 我有一个尺寸为(h * w)180 * 240的UserForm_Initialize() 。向此UserForm_Initialize()添加的控件是基于大量用户输入的UserForm_Initialize()事件,之后将调整用户UserForm_Initialize()大小。 The problem I'm running into is the fact that the visible range of the userform is smaller than the actual range. 我遇到的问题是用户窗体的可见范围小于实际范围。 To demonstrate I've inserted a second userform with the following code: 为了演示,我使用以下代码插入了第二个用户窗体:

Private Sub UserForm_Initialize()
    Dim ctrl As Control
    Dim i As Integer

    For i = 1 To 2
        Set ctrl = Me.Controls.Add("Forms.Label.1")
        With ctrl
            Debug.Print .Name
            .Caption = i
            .BorderStyle = 1
            .Height = 10
            .Width = 10
        End With
    Next i

    Set ctrl = Me.Controls("Label1")
    With ctrl
        .Top = 0
        .Left = 0
    End With

    Set ctrl = Me.Controls("Label2")
    With ctrl
        .Top = Me.Height - .Height
        .Left = Me.Width - .Width
    End With
End Sub

which generates the following userform: 生成以下用户表单: USerform2示例1

The first label sits exactly on the top and left edge of the userform but the second label is nowhere to be seen as it isn't on the visible part of the userform. 第一个标签正好位于用户窗体的顶部和左侧边缘,但是第二个标签却无处可见,因为它不在用户窗体的可见部分。

How do I get the second label to sit exactly on the bottom and right edge of the visible part of the userform like in the image below? 如何获得第二个标签,使其完全位于用户窗体可见部分的底部和右侧边缘,如下图所示? (I've edited the first image to show what I want) (我已经编辑了第一张图片以显示我想要的内容) 在此处输入图片说明

Use the InsideHeight and InsideWidth properties: 使用InsideHeightInsideWidth属性:

With ctrl
    .Top = Me.InsideHeight - .Height
    .Left = Me.InsideWidth - .Width
End With

To change the InsideHeight/InsideWidth you must calculate the actual Height/Width. 要更改InsideHeight / InsideWidth,必须计算实际的Height / Width。 To do that, you need to know the width of each userform border and the height of the titlebar. 为此,您需要知道每个用户窗体边框的宽度和标题栏的高度。 For example, somewhere in the initialization of the userform do 例如,在用户窗体初始化的某个地方

Static FormBorderSize as single, FormTitleSize as single
With Me
  FormBorderSize = (Me.Width-Me.InsideWidth)/2
  FormTitleSize = Me.Height-(Me.InsideHeight+Me.FormBorderSize)
End With

With those two values you can compute the overall Height/Width of the userform to get the desired InsideHeight/InsideWidth. 使用这两个值,您可以计算用户窗体的整体高度/宽度,以获得所需的InsideHeight / InsideWidth。

You can declare these two variables as Private or as Public Properties or as shown here as Static. 您可以将这两个变量声明为“私有”或“公共属性”,或声明为“静态”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM