Cari colleghi,

ho provato il seguente codice per salvare (Sub SaveSizes) le caratteristiche di tutti i controlli del form, per poi riutilizzare queste informazioni nella sub "ResizeControls".

Ho riscontrato due problemi:

1) se i controlli sono creati da codice in runtime, mi dice "Subscript out of range" con debug riferito a "With m_ControlPositions(i)" (codice scritto in verde) per i=2 [ho l'impressione che il problema č rappresentato proprio dalla creazione dei controlli in runtime];

2) se i controlli sono creati manualmente in fase di progettazione, non riscontro l'errore 1) ma dice "Top property cannot be read at run time" (codice scritto in blu).

SE QUALCUNO E' IN GRADO DI SPIEGARMI:
-COME MAI 1) E 2);
-SE E COME E' POSSIBILE OVVIARE, E SOPRATTUTTO
-COME RISOLVERE IL PROBLEMA DI RIDIMENSIONAMENTO DEI CONTROLLI IN RUN TIME.

Ciao a tutti, attendo una vostra.

Giuliano.

Private Type ControlPositionType
Left As Single
Top As Single
Width As Single
Height As Single
FontSize As Single
End Type

Private m_ControlPositions() As ControlPositionType
Private m_FormWid As Single
Private m_FormHgt As Single
Private Sub Form_Resize()
ResizeControls
End Sub
Private Sub Form_Load()
SaveSizes
End Sub

' Save the form's and controls' dimensions.
Private Sub SaveSizes()
Dim i As Integer
Dim ctl As Control

' Save the controls' positions and sizes.
ReDim m_ControlPositions(1 To Controls.Count)
i = 1
For Each ctl In Controls
With m_ControlPositions(i)
If TypeOf ctl Is Line Then
.Left = ctl.X1
.Top = ctl.Y1
.Width = ctl.X2 - ctl.X1
.Height = ctl.Y2 - ctl.Y1
Else
.Top = ctl.Top .Left = ctl.Left
.Width = ctl.Width
.Height = ctl.Height
On Error Resume Next
.FontSize = ctl.Font.Size
On Error GoTo 0
End If
End With
i = i + 1
Next ctl

' Save the form's size.
m_FormWid = ScaleWidth
m_FormHgt = ScaleHeight
End Sub

' Arrange the controls for the new size.
Private Sub ResizeControls()
Dim i As Integer
Dim ctl As Control
Dim x_scale As Single
Dim y_scale As Single

' Don't bother if we are minimized.
If WindowState = vbMinimized Then Exit Sub

' Get the form's current scale factors.
x_scale = ScaleWidth / m_FormWid
y_scale = ScaleHeight / m_FormHgt

' Position the controls.
i = 1
For Each ctl In Controls
With m_ControlPositions(i)
If TypeOf ctl Is Line Then
ctl.X1 = x_scale * .Left
ctl.Y1 = y_scale * .Top
ctl.X2 = ctl.X1 + x_scale * .Width
ctl.Y2 = ctl.Y1 + y_scale * .Height
Else
ctl.Left = x_scale * .Left
ctl.Top = y_scale * .Top
ctl.Width = x_scale * .Width
If Not (TypeOf ctl Is ComboBox) Then
' Cannot change height of ComboBoxes.
ctl.Height = y_scale * .Height
End If
On Error Resume Next
ctl.Font.Size = y_scale * .FontSize
On Error GoTo 0
End If
End With
i = i + 1
Next ctl
End Sub