-
Jul 1st, 2024, 05:35 AM
#1
Thread Starter
Fanatic Member
resize all controls on form dynamically when form is resized, works well, setup easy
This also resizes fonts.
It keeps controls aligned well.
Set your min size in the form designer properties.
The code works to full screen resizing and has seamlessly redrawn control sizes as form edge is dragged real time for me.
Found this online and had to modify some things as it threw some exceptions.
This is how It worked for me.
First, I called the module code as 'clsElasticforms.vb'
then in 3 places these 3 lines
at the top this
Code:
Private clsElastic As New clsElasticForms
somewhere in the form_load this. Here is where it logs all the controls, so place it after you have changed any control properties at run time
Code:
clsElastic.FindAllControls(Me)
Then in the form_resize, put this
Code:
clsElastic.ResizeAllControls(Me)
The module code
Code:
'-------------------------------------------------------------------------------
' Resizer
' This class is used to dynamically resize and reposition all controls on a form.
' Container controls are processed recursively so that all controls on the form
' are handled.
'
' Usage:
' Resizing functionality requires only three lines of code on a form:
'
' 1. Create a form-level reference to the Resize class:
' Dim myResizer as Resizer
'
' 2. In the Form_Load event, call the Resizer class FIndAllControls method:
' myResizer.FindAllControls(Me)
'
' 3. In the Form_Resize event, call the Resizer class ResizeAllControls method:
' myResizer.ResizeAllControls(Me)
'
'-------------------------------------------------------------------------------
Imports System.Data.SqlTypes
Public Class clsElasticForms
'----------------------------------------------------------
' 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)
Private alreadyran As Boolean = False
'----------------------------------------------------------------------------------------
' FindAllControls
' Recursive function to process all controls contained in the initially passed
' control container and store it in the Control dictionary
'----------------------------------------------------------------------------------------
Public Sub FindAllControls(ByVal thisCtrl As Control)
' If Already run once, Then don't run again the clear
' If alreadyran Then
'ctrlDict.Clear()
'alreadyran = True
'End If
'-- 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
'changed 6/25/2024, adding this IF gets rid of error "An item of same key has already been added".
If Not ctrlDict.ContainsKey(c.name) Then
ctrlDict.Add(c.name, c)
' If c.name = "Label1" Then Stop
' Debug.Print(c.name)
' Debug.Print(c.GetType())
End If
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(ByVal 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
'changed 6/25/2024 to prevent dividing by ZERO
'If ctl.Name = "Label100" Then
'Debug.Print(ctl.Name)
'Debug.Print(ctl.Height)
'Debug.Print(c.originalHeight)
'Debug.Print(ctl.Width)
'Debug.Print(c.originalWidth)
'End If
If c.originalWidth = 0 Then c.originalWidth = 1
If ctl.Width = 0 Then ctl.Width = 1
If c.originalHeight = 0 Then c.originalHeight = 1
If ctl.Height = 0 Then ctl.Height = 1
'dont do the fonts for any ctl like a rtb?
'where the size can be set in the program
'If TypeOf ctl IsNot RichTextBox Then
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
End If
Catch
End Try
End If
Catch ex As Exception
' Show the exception's message.
Debug.WriteLine(ex.Message)
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
End Class
-
Jul 1st, 2024, 05:46 AM
#2
Thread Starter
Fanatic Member
Re: resize all controls on form dynamically when form is resized, works well, setup e
You can get rid of the comments and the alreadyran dim. I was figuring out how it worked, and you can exclude certain controls from resizing by control name or type of control.
As it is, it resizes all controls on a form.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|