Click here to Skip to main content
15,888,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

Good Day to All,

I am working in a windows form application in vb.net using visual studio 2015.
I need to make auto sizes of all the controls placed on the form like Labels, Text Boxes, Combo Boxes, DataGridViews etc.

For example when the sizes of the form increases or decreases then the size of the controls also must be increase or decrease with the font sizes accordingly.
Moreover if the application is running on a system that has a high resolution and big display screen then the form will be adjusted automatically because the Windowstate Property of the for is set as Maximized. In that case i want that the sizes of the controls and their font sizes should also be increased accordingly and the vice versa.

Hopefully i successfully explained what i need actually.
If anyone can guide me i will be grateful for this ac of kind.

Thanks!

Regards:
Muhammad Asim Mughal

What I have tried:

For this purpose i used Table Layout Panel Control and also used the Anchor & Doc property of the controls. So the sizes of the controls are increased and decreased but the font sizes remains same so that's why the resolution of the Form looks UN-balanced.
Posted
Updated 7-Feb-18 20:35pm

1 solution

There are 3 ways to do it :
1st : You customize each of your controls to make it able to fit it's Fontsize to the Controls dimension
2nd : You customize your Container-Control to do the same with the Controls belonging to it
3rd : You add a method to your Form (which is also a Container) to do that with the Controls belonging to it.

Now you should decide if you want to have the maximum Fontsize for each of those Controls (one Control has Fontsize=16 and one has 12 and so on) ... OR ... if you want to have the maximum Fontsize which is common to the Controls (one Control has Fontsize=16 and one has 12 - so you set each to 12).

The way to realize it is :
- iterate to the Controls-Collection of your Container
- get the Size of each Control and it's Text-Content and calculate the maximum possible Fontsize of this Control and memorize it (depending on your decision)
- ::
- iterate to the Controls-Collection of your Container
- set the Size of each Control to the new (calculated) Fontsize

Here is a method to get the maximum matching Fontsize for a given Size of a Control depending on it's Text-Content and the selected Font and it's Style :
VB
Function GetFontSizeMatch(ByVal myText As String, ByVal myFont As Font, ByVal mySize As Size) As Single

        If Trim(myText).Length <= 0 Then myText = "X"

        Dim currentSize As Single = CSng(myFont.Size)
        Dim workFont As Font = New Font(myFont.Name, currentSize, myFont.Style, myFont.Unit)
        Dim myTextSize As SizeF

        If (mySize.Width >= 1) AndAlso (mySize.Height >= 1) Then
            Do
                currentSize += 4.0 : If currentSize > 50.0 Then Exit Do
                workFont = New Font(workFont.Name, currentSize, workFont.Style, workFont.Unit)
                myTextSize = TextRenderer.MeasureText(myText, workFont)
            Loop Until (myTextSize.Width > mySize.Width) Or (myTextSize.Height > mySize.Height)

            Do
                currentSize -= 0.5 : If currentSize < 5.0 Then Exit Do
                workFont = New Font(workFont.Name, currentSize, workFont.Style, workFont.Unit)
                myTextSize = TextRenderer.MeasureText(myText, workFont)
            Loop Until (myTextSize.Width <= mySize.Width) AndAlso (myTextSize.Height <= mySize.Height)

        End If

        Return currentSize

    End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900