I was experimenting with an idea I had and I came out with a control to resize and reposition all the other controls on the form.
In this example I have two projects, the first one is a regular windows form with several controls and containers that will adjust automatically to another screen resolution. It also contains on the tools menu and on one button a code to check if it is really working. You could adapt the functions to your particular situation.
The other project is a DLL, based on the same code. It only needs to be dropped on the form and it will do all the job. I didn't allow for adjusting the font.size property of the controls because it is read-only and you need to create new fonts at runtime, and they could be of any font family of your choice, and the sizes discrete.
For the project #1:
I created an arraylist that will contain all the controls as object, not the controls names. And variables to contain the "WIDTH" and "HEIGHT" resolution of the screen, and to contain the ratios of the resolution change between two different computers.
Imports System.Collections 'For arraylist
Public Class Form1
Private CtlArray As New ArrayList
Dim intX, intY As Integer
Dim Xratio, Yratio As Single
.
.
.
Then these variables are filled in the form load event.
Also in the form load event, we will add to the arraylist the primary controls in the form. And then call a subroutine that will check for the container controls that might have children of their own.
This subroutine, "GetTheChildren()" will call other subroutines.
After all the controls have been accounted for then the resize subroutine is called.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Screen Resolution values on running computer
intX = Screen.PrimaryScreen.Bounds.Width
intY = Screen.PrimaryScreen.Bounds.Height
'These are design screen resolutions, but should work with other resolutions too
'You should design at low resolution, so components will grow and not shrink when taken
'to other computers. I haven't check for font size, but by growing the components you won't have
'a problem.
Xratio = intX / 1152
Yratio = intY / 864
'Get the controls on the form, including menus, but not the controls in other containers
For Each Cnt As Control In Me.Controls
CtlArray.Add(Cnt)
Next
'Get the children controls
GetTheChildren()
'Adjust New size and position
ResizeThem()
End Sub
This subroutine:
Private Sub GetTheChildren()
'Gets the controls inside containes like panels or tabcontrols
'For Each ctl As Control In GetAllControls(Me.Parent)
For Each ctl As Control In GetAllControls(Me)
If ctl.Parent IsNot Me Then
If TypeOf ctl.Parent Is TabPage Then
If ctl.Name = "" Then
CtlArray.Add(ctl)
Else
CtlArray.Add(ctl)
End If
Else
If Not TypeOf (ctl) Is TabPage Then
If ctl.Name = "" Then
CtlArray.Add(ctl)
Else
CtlArray.Add(ctl)
End If
End If
End If
End If
Next
End Sub
calls a function, "GetAllControls" that will cycle through the controls and will find the controls of type container.
Function GetAllControls(ByVal container As Control) As Control()
Dim al As New ArrayList
Dim ctl As Control
For Each ctl In container.Controls
GetControlsWithChildren(ctl, al)
Next
Return al.ToArray(GetType(Control))
End Function
GetAllcontrols calls a recursive subroutine "GetControlsWithChildren" that will find all the other controls embedded on a container, IE: panel, tabcontrol, splitcontainer:
Sub GetControlsWithChildren(ByVal container As Control, ByVal al As ArrayList)
' add this control to the ArrayList
al.Add(container)
' add all its child controls, by calling this routine recursively
Dim ctl As Control
For Each ctl In container.Controls
'A TabPage is a Panel; SplitContainer is a Panel
GetControlsWithChildren(ctl, al)
Next
End Sub
At the end of form load "ResizeThem". This subroutine will look at the controls on the arraylist and, after checking it's type, will adjust the LEFT, TOP, WIDTH, HEIGHT, DOCK and AUTOSIZE of the control.
Private Sub ResizeThem()
Dim i As Integer
For i = 0 To CtlArray.Count - 1
If TypeOf CtlArray.Item(i) Is MenuStrip Then
Else
If TypeOf CtlArray.Item(i) Is Panel And CtlArray.Item(i).parent IsNot Me Then
'SplitPanel for instance
Else
CtlArray.Item(i).autosize = False
CtlArray.Item(i).dock = 0
CtlArray.Item(i).width = CtlArray.Item(i).width * Xratio
CtlArray.Item(i).left = CtlArray.Item(i).left * Xratio
CtlArray.Item(i).height = CtlArray.Item(i).height * Yratio
CtlArray.Item(i).top = CtlArray.Item(i).top * Yratio
End If
End If
Next
End Sub
In this example I provided and extra subroutine, with a button and also in the menu, that will adjust size and position by 5%. This is just like a test to show that it really works.
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click, Button1.Click
'This subroutine is just to check if the resizing will actually take effect.
Dim i As Integer
For i = 0 To CtlArray.Count - 1
If TypeOf CtlArray.Item(i) Is MenuStrip Then
Else
If TypeOf CtlArray.Item(i) Is Panel And CtlArray.Item(i).parent IsNot Me Then
'SplitPanel for instance
Else
CtlArray.Item(i).autosize = False
CtlArray.Item(i).dock = 0
CtlArray.Item(i).width = CtlArray.Item(i).width * 1.05
CtlArray.Item(i).left = CtlArray.Item(i).left * 1.05
CtlArray.Item(i).height = CtlArray.Item(i).height * 1.05
CtlArray.Item(i).top = CtlArray.Item(i).top * 1.05
End If
End If
Next
End Sub
The second project is the DLL. It is the same code. Except that instead of calling the routines with "ME", it calls them with "ME.PARENT" and doesn't have the resizing subroutine:
Project #2:
Imports System.Collections
Imports System.Windows.Forms
Public Class Size
Private CtlArray As New ArrayList
Dim intX, intY As Integer
Dim Xratio, Yratio As Single
Private Sub SizeNPos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
intX = Windows.Forms.Screen.PrimaryScreen.Bounds.Width
intY = Screen.PrimaryScreen.Bounds.Height
'These are my design screen resolutions, but should work with other resolutions too
Xratio = intX / 1152
Yratio = intY / 864
'Gets the controls on the form, including menus, but not the controls in other containers
For Each Cnt As Control In Me.Parent.Controls
CtlArray.Add(Cnt)
Next
'Get the children controls
GetTheChildren()
'Adjust New size and position
ResizeThem()
End Sub
Function GetAllControls(ByVal container As Control) As Control()
Dim al As New ArrayList
Dim ctl As Control
For Each ctl In container.Controls
GetControlsWithChildren(ctl, al)
Next
Return al.ToArray(GetType(Control))
End Function
Sub GetControlsWithChildren(ByVal container As Control, ByVal al As ArrayList)
' add this control to the ArrayList
al.Add(container)
' add all its child controls, by calling this routine recursively
Dim ctl As Control
For Each ctl In container.Controls
'A TabPage is a Panel; SplitContainer is a Panel
GetControlsWithChildren(ctl, al)
Next
End Sub
Private Sub GetTheChildren()
'Gets the controls inside containes like panels or tabcontrols
'For Each ctl As Control In GetAllControls(Me.Parent)
For Each ctl As Control In GetAllControls(Me.Parent)
If ctl.Parent IsNot Me Then
If TypeOf ctl.Parent Is TabPage Then
If ctl.Name = "" Then
CtlArray.Add(ctl)
Else
CtlArray.Add(ctl)
End If
Else
If Not TypeOf (ctl) Is TabPage Then
If ctl.Name = "" Then
CtlArray.Add(ctl)
Else
CtlArray.Add(ctl)
End If
End If
End If
End If
Next
End Sub
Private Sub ResizeThem()
Dim i As Integer
For i = 0 To CtlArray.Count - 1
If TypeOf CtlArray.Item(i) Is MenuStrip Then
Else
If TypeOf CtlArray.Item(i) Is Panel And CtlArray.Item(i).parent IsNot Me Then
'SplitPanel for instance
Else
CtlArray.Item(i).autosize = False
CtlArray.Item(i).dock = 0
CtlArray.Item(i).width = CtlArray.Item(i).width * Xratio
CtlArray.Item(i).left = CtlArray.Item(i).left * Xratio
CtlArray.Item(i).height = CtlArray.Item(i).height * Yratio
CtlArray.Item(i).top = CtlArray.Item(i).top * Yratio
End If
End If
Next
End Sub
End Class
Please check the code and give me some feedback.
Thank you,
ricardosms.
Adjust Control's Size-Position With Screen Resolution.zip (185.99K)
Number of downloads: 1568





MultiQuote




|