簡體   English   中英

無法根據表單大小調整控件大小

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM