[英]Not able to resize controls accordig to form size
我根据屏幕大小调整了表单大小:
Me.width = Screen.width
Me.height = Screen.height
此外,我想根据我制作的表格大小调整控件的大小。
我尝试了以下循环:
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
它给我的错误: left property can not be read runtime.
Plz帮助我。
如果你的控件没有任何容器(如计时器,......),就会发生这种情况。
检查控件类型以防止在没有容器的情况下调整constols的大小。
If Not TypeOf tmpControl Is Timer Then
...
End If
我假设此代码与其他代码在一个模块中,您可能有也可能没有现有的错误处理。 Ali Mousavi Kherad是正确的,当他说当你试图在没有容器的情况下设置控件的位置时会产生错误。 您的代码可能会生成您想要更正的错误,例如tmpControl.Left = tmpControl.Left / PrevResizeX * Me.ScaleWidth
如果数字太大, tmpControl.Left = tmpControl.Left / PrevResizeX * Me.ScaleWidth
可能会导致溢出错误。 我建议将left,top,width和height属性定义为变量,并在将它们分配给控件之前执行某种测试。 然后,设置控件的位置可以将代码包装在On Error Resume Next
语句中。 如果此时产生错误,可能是因为控件类似于定时器控件,并且您不关心它的位置是否无法设置。
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.