简体   繁体   English

无法根据表单大小调整控件大小

[英]Not able to resize controls accordig to form size

I have resized my form according to screen size as: 我根据屏幕大小调整了表单大小:

Me.width = Screen.width
Me.height = Screen.height

Further i wanted to resize my controls according to the form size i made. 此外,我想根据我制作的表格大小调整控件的大小。

I tried following loop for it: 我尝试了以下循环:

For Each tmpControl In frm.Controls
        If PrevResizeX = Empty Then
        PrevResizeX = 1
        End If
        If PrevResizeY = Empty Then
        PrevResizeY = 1
        End If
        tmpControl.Left = tmpControl.Left / PrevResizeX * Me.ScaleWidth
        tmpControl.Top = tmpControl.Top / PrevResizeY * Me.ScaleHeight
        tmpControl.width = tmpControl.width / PrevResizeX * Me.ScaleWidth
        tmpControl.height = tmpControl.height / PrevResizeY * Me.ScaleHeight

 Next tmpControl

Its giving me error: left property can not be read runtime. 它给我的错误: left property can not be read runtime.

Plz help me. Plz帮助我。

This happens when you have a control that dont has any container (like timer,...). 如果你的控件没有任何容器(如计时器,......),就会发生这种情况。
Check the type of control to prevent to resize the constols with no container. 检查控件类型以防止在没有容器的情况下调整constols的大小。

If Not TypeOf tmpControl Is Timer Then
...
End If

I am assuming this code is in a module with other code and you may or may not have existing error handling. 我假设此代码与其他代码在一个模块中,您可能有也可能没有现有的错误处理。 Ali Mousavi Kherad is correct when he says the error is generated when you try to set the postion of a control without a container. Ali Mousavi Kherad是正确的,当他说当你试图在没有容器的情况下设置控件的位置时会产生错误。 Your code might generate errors that you want to correct, for instance tmpControl.Left = tmpControl.Left / PrevResizeX * Me.ScaleWidth could cause an overflow error if the number gets too large. 您的代码可能会生成您想要更正的错误,例如tmpControl.Left = tmpControl.Left / PrevResizeX * Me.ScaleWidth如果数字太大, tmpControl.Left = tmpControl.Left / PrevResizeX * Me.ScaleWidth可能会导致溢出错误。 I would suggest setting the defining the left, top, width, and height properties as variables and performing some sort of test before assigning them to the control. 我建议将left,top,width和height属性定义为变量,并在将它们分配给控件之前执行某种测试。 Then far as the setting the position of the controls you can wrap the code in an On Error Resume Next statement. 然后,设置控件的位置可以将代码包装在On Error Resume Next语句中。 If an error is generated at that point it is probably because the control is something like a timer control and you don't care if it's position can't be set. 如果此时产生错误,可能是因为控件类似于定时器控件,并且您不关心它的位置是否无法设置。

Dim tmpControl As Control
Dim PrevResizeX As Integer
Dim PrevResizeY As Integer
Dim lngLeft as Long
Dim lngTop as Long
Dim lngWidth as Long
Dim lngHeight as Long

For Each tmpControl In Me.Controls
    If PrevResizeX = Empty Then
        PrevResizeX = 1
    End If
    If PrevResizeY = Empty Then
        PrevResizeY = 1
    End If
    lngLeft = tmpControl.Left / PrevResizeX * Me.ScaleWidth
    lngTop = tmpControl.Top / PrevResizeY * Me.ScaleHeight
    lngWidth = tmpControl.Width / PrevResizeX * Me.ScaleWidth
    lngHeight = tmpControl.Height / PrevResizeY * Me.ScaleHeight

    ' do some bounds checking here
    ' if everything is okay try to assign the new position to the control
    On Error Resume Next   ' ignore any error the Move method generates
    tmpControl.Move lngLeft, lngTop, lngWidth, lngHeight
    On Error GoTo 0   ' cancel the On Error Resume Next statement
Next tmpControl

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

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