Advertisement
hungcoder92

VB.NET - Dynamically Resize and Reposition All Controls when Form is Resized, Including Font Sizes

Apr 30th, 2022
1,905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.12 KB | None | 0 0
  1. '-------------------------------------------------------------------------------
  2. ' Resizer
  3. ' This class is used to dynamically resize and reposition all controls on a form.
  4. ' Container controls are processed recursively so that all controls on the form
  5. ' are handled.
  6. '
  7. ' Usage:
  8. '  Copy by HungVB.Com:
  9. '
  10. '  1. Create a form-level reference to the Resize class:
  11. '     Dim myResizer as Resizer
  12. '
  13. '  2. In the Form_Load event, call the  Resizer class FIndAllControls method:
  14. '     myResizer.FindAllControls(Me)
  15. '
  16. '  3. In the Form_Resize event, call the  Resizer class ResizeAllControls method:
  17. '     myResizer.ResizeAllControls(Me)
  18. '
  19. '-------------------------------------------------------------------------------
  20. Public Class Resizer
  21.  
  22.     '----------------------------------------------------------
  23.     ' ControlInfo
  24.     ' Structure of original state of all processed controls
  25.     '----------------------------------------------------------
  26.     Private Structure ControlInfo
  27.         Public name As String
  28.         Public parentName As String
  29.         Public leftOffsetPercent As Double
  30.         Public topOffsetPercent As Double
  31.         Public heightPercent As Double
  32.         Public originalHeight As Integer
  33.         Public originalWidth As Integer
  34.         Public widthPercent As Double
  35.         Public originalFontSize As Single
  36.     End Structure
  37.  
  38.     '-------------------------------------------------------------------------
  39.     ' ctrlDict
  40.     ' Dictionary of (control name, control info) for all processed controls
  41.     '-------------------------------------------------------------------------
  42.     Private ctrlDict As Dictionary(Of String, ControlInfo) = New Dictionary(Of String, ControlInfo)
  43.  
  44.     '----------------------------------------------------------------------------------------
  45.     ' FindAllControls
  46.     ' Recursive function to process all controls contained in the initially passed
  47.     ' control container and store it in the Control dictionary
  48.     '----------------------------------------------------------------------------------------
  49.     Public Sub FindAllControls(thisCtrl As Control)
  50.  
  51.         '-- If the current control has a parent, store all original relative position
  52.         '-- and size information in the dictionary.
  53.         '-- Recursively call FindAllControls for each control contained in the
  54.         '-- current Control
  55.         For Each ctl As Control In thisCtrl.Controls
  56.             Try
  57.                 If Not IsNothing(ctl.Parent) Then
  58.                     Dim parentHeight = ctl.Parent.Height
  59.                     Dim parentWidth = ctl.Parent.Width
  60.  
  61.                     Dim c As New ControlInfo
  62.                     c.name = ctl.Name
  63.                     c.parentName = ctl.Parent.Name
  64.                     c.topOffsetPercent = Convert.ToDouble(ctl.Top) / Convert.ToDouble(parentHeight)
  65.                     c.leftOffsetPercent = Convert.ToDouble(ctl.Left) / Convert.ToDouble(parentWidth)
  66.                     c.heightPercent = Convert.ToDouble(ctl.Height) / Convert.ToDouble(parentHeight)
  67.                     c.widthPercent = Convert.ToDouble(ctl.Width) / Convert.ToDouble(parentWidth)
  68.                     c.originalFontSize = ctl.Font.Size
  69.                     c.originalHeight = ctl.Height
  70.                     c.originalWidth = ctl.Width
  71.                     ctrlDict.Add(c.name, c)
  72.                 End If
  73.  
  74.             Catch ex As Exception
  75.                 Debug.Print(ex.Message)
  76.             End Try
  77.  
  78.             If ctl.Controls.Count > 0 Then
  79.                 FindAllControls(ctl)
  80.             End If
  81.  
  82.         Next '-- For Each
  83.  
  84.     End Sub
  85.  
  86.     '----------------------------------------------------------------------------------------
  87.     ' ResizeAllControls
  88.     ' Recursive function to resize and reposition all controls contained in the Control
  89.     ' dictionary
  90.     '----------------------------------------------------------------------------------------
  91.     Public Sub ResizeAllControls(thisCtrl As Control)
  92.  
  93.         Dim fontRatioW As Single
  94.         Dim fontRatioH As Single
  95.         Dim fontRatio As Single
  96.         Dim f As Font
  97.  
  98.         '-- Resize and reposition all controls in the passed control
  99.         For Each ctl As Control In thisCtrl.Controls
  100.             Try
  101.                 If Not IsNothing(ctl.Parent) Then
  102.                     Dim parentHeight = ctl.Parent.Height
  103.                     Dim parentWidth = ctl.Parent.Width
  104.  
  105.                     Dim c As New ControlInfo
  106.  
  107.                     Dim ret As Boolean = False
  108.                     Try
  109.                         '-- Get the current control's info from the control info dictionary
  110.                         ret = ctrlDict.TryGetValue(ctl.Name, c)
  111.  
  112.                         '-- If found, adjust the current control based on control relative
  113.                         '-- size and position information stored in the dictionary
  114.                         If (ret) Then
  115.                             '-- Size
  116.                             ctl.Width = Int(parentWidth * c.widthPercent)
  117.                             ctl.Height = Int(parentHeight * c.heightPercent)
  118.  
  119.                             '-- Position
  120.                             ctl.Top = Int(parentHeight * c.topOffsetPercent)
  121.                             ctl.Left = Int(parentWidth * c.leftOffsetPercent)
  122.  
  123.                             '-- Font
  124.                             f = ctl.Font
  125.                             fontRatioW = ctl.Width / c.originalWidth
  126.                             fontRatioH = ctl.Height / c.originalHeight
  127.                             fontRatio = (fontRatioW +
  128.                             fontRatioH) / 2 '-- average change in control Height and Width
  129.                             ctl.Font = New Font(f.FontFamily,
  130.                             c.originalFontSize * fontRatio, f.Style)
  131.  
  132.                         End If
  133.                     Catch
  134.                     End Try
  135.                 End If
  136.             Catch ex As Exception
  137.             End Try
  138.  
  139.             '-- Recursive call for controls contained in the current control
  140.             If ctl.Controls.Count > 0 Then
  141.                 ResizeAllControls(ctl)
  142.             End If
  143.  
  144.         Next '-- For Each
  145.     End Sub
  146.  
  147. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement