Results 1 to 7 of 7

Thread: Resize Form Controls !!!!!!!!!!!!

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    32

    Resize Form Controls !!!!!!!!!!!!

    I have ( as first shot ) a small class that resizes my controls in the main form of me vb.net app. I can use this stupit anchors from .net.
    The only problem is that this class is a bit slow, not to much, but i think it could be better !
    This is me challenge for everyone here in this forum to help me speed up this class. Or maybe someone has a much better idea.

    This is the class (forget the error handling) :

    VB Code:
    1. Imports System.Drawing
    2. Imports System.Convert
    3. Imports System.Windows.Forms
    4.  
    5. Public Class clsResize
    6.     Implements IDisposable
    7.  
    8. #Region " Declare private vars "
    9.  
    10.     Private blnDisposed As Boolean
    11.     Private objFormSize As Size
    12.     Private colControls As New Collection()
    13.     Private Structure Properties
    14.         Dim Left As Integer
    15.         Dim Top As Integer
    16.         Dim Width As Integer
    17.         Dim Height As Integer
    18.         Dim FontSize As Single
    19.     End Structure
    20.  
    21. #End Region
    22.  
    23.  
    24.  
    25. #Region " Initialize and dispose "
    26.  
    27.     Public Sub New(ByRef frm As Form)
    28.         ' initialize the resize class by collecting control properties
    29.         Dim objControl As Control
    30.         Dim stuCtrlProp As Properties
    31.         ' store original form size
    32.         objFormSize = frm.Size
    33.         ' for controls who don't support all the properties resume on error
    34.         On Error Resume Next
    35.         For Each objControl In frm.Controls
    36.             With stuCtrlProp
    37.                 .Left = objControl.Left
    38.                 .Top = objControl.Top
    39.                 .Width = objControl.Width
    40.                 .Height = objControl.Height
    41.                 .FontSize = objControl.Font.Size
    42.             End With
    43.             colControls.Add(stuCtrlProp, objControl.Name)
    44.         Next
    45.         ' clean up
    46.         objControl = Nothing
    47.         ' we made a new instance so set blnDisposed to false
    48.         blnDisposed = False
    49.     End Sub
    50.  
    51.     Public Sub Dispose() Implements IDisposable.Dispose
    52.         ' clean up objects
    53.         If Not blnDisposed Then
    54.             objFormSize = Nothing
    55.             colControls = Nothing
    56.             blnDisposed = True
    57.         End If
    58.     End Sub
    59.  
    60. #End Region
    61.  
    62.  
    63.  
    64. #Region " Resize the from "
    65.  
    66.     Public Sub Resize(ByRef frm As Form)
    67.         Dim dblRatioX As Double
    68.         Dim dblRatioY As Double
    69.         Dim intWinMax As Integer
    70.         Dim objControl As Control
    71.         Dim stuCtrlProp As Properties
    72.         ' setup scaling ratios
    73.         intWinMax = 40
    74.         dblRatioY = 1.0# * (objFormSize.Height - intWinMax) / (frm.Height - intWinMax)
    75.         dblRatioX = 1.0# * objFormSize.Width / frm.Width
    76.         ' for comboboxes, timeres and other nonsizible controls resume on error next
    77.         On Error Resume Next
    78.         ' reposition and resize the controls
    79.         For Each objControl In frm.Controls
    80.             stuCtrlProp = CType(colControls.Item(objControl.Name), Properties)
    81.             With stuCtrlProp
    82.                 objControl.SetBounds(ToInt32(.Left / dblRatioX), ToInt32(.Top / dblRatioY), ToInt32(.Width / dblRatioX), ToInt32(.Height / dblRatioY))
    83.                 objControl.Font = New Font(objControl.Font.Name, ToSingle(.FontSize / dblRatioX) + ToSingle(.FontSize / dblRatioX) Mod 2)
    84.             End With
    85.         Next
    86.         ' clean up
    87.         objControl = Nothing
    88.     End Sub
    89.  
    90. #End Region
    91.  
    92.  
    93. End Class
    Last edited by Josch; Oct 20th, 2002 at 04:39 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width