none
Resize controls and fonts at runtime when objects are resized RRS feed

  • Question

  • I have this class that resizes my controls at runtime when the form is resized

    Public Class Resizer
        '----------------------------------------------------------
        ' ControlInfo
        ' Structure of original state of all processed controls
        '----------------------------------------------------------
        Private Structure ControlInfo
            Public name As String
            Public parentName As String
            Public leftOffsetPercent As Double
            Public topOffsetPercent As Double
            Public heightPercent As Double
            Public originalHeight As Integer
            Public originalWidth As Integer
            Public widthPercent As Double
            Public originalFontSize As Single
        End Structure
    
        '-------------------------------------------------------------------------
        ' ctrlDict
        ' Dictionary of (control name, control info) for all processed controls
        '-------------------------------------------------------------------------
        Private ctrlDict As Dictionary(Of String, ControlInfo) = New Dictionary(Of String, ControlInfo)
    
        '----------------------------------------------------------------------------------------
        ' FindAllControls
        ' Recursive function to process all controls contained in the initially passed
        ' control container and store it in the Control dictionary
        '----------------------------------------------------------------------------------------
        Public Sub FindAllControls(thisCtrl As Control)
    
            '-- If the current control has a parent, store all original relative position
            '-- and size information in the dictionary.
            '-- Recursively call FindAllControls for each control contained in the
            '-- current Control
            For Each ctl As Control In thisCtrl.Controls
                Try
                    If Not IsNothing(ctl.Parent) Then
                        Dim parentHeight = ctl.Parent.Height
                        Dim parentWidth = ctl.Parent.Width
    
                        Dim c As New ControlInfo
                        c.name = ctl.Name
                        c.parentName = ctl.Parent.Name
                        c.topOffsetPercent = Convert.ToDouble(ctl.Top) / Convert.ToDouble(parentHeight)
                        c.leftOffsetPercent = Convert.ToDouble(ctl.Left) / Convert.ToDouble(parentWidth)
                        c.heightPercent = Convert.ToDouble(ctl.Height) / Convert.ToDouble(parentHeight)
                        c.widthPercent = Convert.ToDouble(ctl.Width) / Convert.ToDouble(parentWidth)
                        c.originalFontSize = ctl.Font.Size
                        c.originalHeight = ctl.Height
                        c.originalWidth = ctl.Width
                        ctrlDict.Add(c.name, c)
                    End If
    
                Catch ex As Exception
                    Debug.Print(ex.Message)
                End Try
    
                If ctl.Controls.Count > 0 Then
                    FindAllControls(ctl)
                End If
    
            Next '-- For Each
    
        End Sub
    
        '----------------------------------------------------------------------------------------
        ' ResizeAllControls
        ' Recursive function to resize and reposition all controls contained in the Control
        ' dictionary
        '----------------------------------------------------------------------------------------
        Public Sub ResizeAllControls(thisCtrl As Control)
    
            Dim fontRatioW As Single
            Dim fontRatioH As Single
            Dim fontRatio As Single
            Dim f As Font
    
            '-- Resize and reposition all controls in the passed control
            For Each ctl As Control In thisCtrl.Controls
                Try
                    If Not IsNothing(ctl.Parent) Then
                        Dim parentHeight = ctl.Parent.Height
                        Dim parentWidth = ctl.Parent.Width
    
                        Dim c As New ControlInfo
    
                        Dim ret As Boolean = False
                        Try
                            '-- Get the current control's info from the control info dictionary
                            ret = ctrlDict.TryGetValue(ctl.Name, c)
    
                            '-- If found, adjust the current control based on control relative
                            '-- size and position information stored in the dictionary
                            If (ret) Then
                                '-- Size
                                ctl.Width = Int(parentWidth * c.widthPercent)
                                ctl.Height = Int(parentHeight * c.heightPercent)
    
                                '-- Position
                                ctl.Top = Int(parentHeight * c.topOffsetPercent)
                                ctl.Left = Int(parentWidth * c.leftOffsetPercent)
    
                                '-- Font
                                f = ctl.Font
                                fontRatioW = ctl.Width / c.originalWidth
                                fontRatioH = ctl.Height / c.originalHeight
                                fontRatio = (fontRatioW +
                                fontRatioH) / 2 '-- average change in control Height and Width
                                ctl.Font = New Font(f.FontFamily,
                                c.originalFontSize * fontRatio, f.Style)
    
                            End If
                        Catch
                        End Try
                    End If
                Catch ex As Exception
                End Try
    
                '-- Recursive call for controls contained in the current control
                If ctl.Controls.Count > 0 Then
                    ResizeAllControls(ctl)
                End If
    
            Next '-- For Each
        End Sub
    This class is doing most of what I expect except that it is not resizing the font sizes on my Labels. What adjustments can I make so that it can resize the font size on my label. I came across this code from here . How can I resize the font size of my labels when the form is resized


    If you think it you can achieve it

    Saturday, May 27, 2017 10:46 AM

Answers

  • Hi

    I have used the code as per IronRazerz from this thread. Used in a couple of projects and works a treat.

    Resize scaling


    Regards Les, Livingston, Scotland

    • Proposed as answer by Frank L. Smith Saturday, May 27, 2017 1:22 PM
    • Marked as answer by tendaimare Tuesday, May 30, 2017 10:47 AM
    Saturday, May 27, 2017 12:31 PM

All replies

  • Hi

    I have used the code as per IronRazerz from this thread. Used in a couple of projects and works a treat.

    Resize scaling


    Regards Les, Livingston, Scotland

    • Proposed as answer by Frank L. Smith Saturday, May 27, 2017 1:22 PM
    • Marked as answer by tendaimare Tuesday, May 30, 2017 10:47 AM
    Saturday, May 27, 2017 12:31 PM
  •  I have also posted anther similar but,  different example in the link below.  It works slightly different than my example that leshay linked to.  It is only good if you are using it on a Form that has a Fixed border style which can not be resized except for minimizing and maximizing it.  8)

    Vb.Net Question:  fitting the form


    If you say it can`t be done then i`ll try it

    Saturday, May 27, 2017 8:06 PM
  • Yea maybe you succeed in that because nobody in 25 years did. 

    The controls you use are a part of Windows, not a kind of painting on the screen.


    Success
    Cor

    Saturday, May 27, 2017 8:12 PM