简体   繁体   中英

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.

Plz help me.

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.

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. 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. 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. Then far as the setting the position of the controls you can wrap the code in an On Error Resume Next statement. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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