Resizing text in all the project

1qaz2wsx7

Member
Joined
Aug 16, 2007
Messages
17
Programming Experience
Beginner
Hi :)

I have a mdi form with caple of child forms, and i want
to add the capbillity to resize the text size of all the forms
and all the components inside them, how can i do that ?

Thanks.
 
Last edited:
What exactly do ya mean by resize everything on all the forms?

Do you mean make the form bigger and everything resizes accordingly? Or do you mean set all the fonts larger?
 
Hi :)

I ment set all the fonts larger.
Make all the texts, in all the forms and components inside them, larger.


Thanks.
 
Last edited:
I'm not an expert in MDI stuff, but in the parent form you can put this code in

VB.NET:
    Private Sub ChangeFontSize(ByVal FontSize As Single)
        For Each objForm As Form In Me.MdiChildren
            Dim currentFont As Font = objForm.Font
            objForm.Font = New Font(currentFont.FontFamily, FontSize, currentFont.Style, currentFont.Unit)
        Next
    End Sub

And then in the child forms you can handle the font changed event as such

VB.NET:
    Private Sub Form2_FontChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FontChanged
        Dim sngSize As Single = Me.Font.Size
        For Each objControl As Control In Me.Controls
            Dim currentFont As Font = objControl.Font
            objControl.Font = New Font(currentFont.FontFamily, sngSize, currentFont.Style, currentFont.Unit)
        Next
    End Sub

Here is the code I used in the parent form to test this.

VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim frm1, frm2 As New Form2
        frm1.MdiParent = Me
        frm2.MdiParent = Me
        frm1.Show()
        frm2.Show()
        ChangeFontSize(16)
    End Sub

Is this what you want?
 
Hi :)

THanks for the answer, but this is not good for me.

I have caple of child forms, and each one contain diffrent things,
like labels, text box, datagrids etc.
What i want to do is to just resize all the font size in every text that exist in the project, including the text fields, labels, cell font size in a data grid, everything.

Is there a way to do that ?


Thanks.
 
Most controls inherit the Control class, so they implement some the Font Property to a certain extent. Some things that are more specialized may require tweaking. DataGrids could be one example of this.

As an alternative you could pass Me.Controls to a function external to any form and then just implement that one function. That would reduce code duplication. You can test the control for being something more specialized as well.

VB.NET:
            If TypeOf (objControl) Is DataGrid Then
                Dim objDataGrid As DataGrid = CType(objControl, DataGrid)
                'implement any DataGrid specific stuff
            End If
 
Hi :)


The code you gave is not working well.

For some reason the code you gave at the beginning is making
my child form larger :/


I tryed this:
For Each objForm As Form In Me.MdiChildren
For Each cntrl As Control In objForm
Dim currentFont As Font = objForm.Font
cntrl.Font = New Font(currentFont.FontFamily, 20, currentFont.Style, currentFont.Unit)
Next
Next

But objForm is not a collection, so how can i do that ?



Thanks.
 
Use the following instead

VB.NET:
        For Each objForm As Form In Me.MdiChildren
            Dim currentFont As Font = objForm.Font
            objForm.Font = New Font(currentFont.FontFamily, FontSize, currentFont.Style, currentFont.Unit)
            For Each objControl As Control In objForm.Controls
                objControl.Font = New Font(currentFont.FontFamily, FontSize, currentFont.Style, currentFont.Unit)
            Next
        Next

The child forms are being made larger because of the AutoScaleMode Property of the Child form is set to Font. Change it to None to fix the problem.

Just like in the code above. You must use the Form.Controls property to get the controls on the form.
 
Hi :)

Thanks its working.

But i have another problem.
In some of the child forms, i have TabControl with several pages,
the Code you gave, changing the TabControl tabs, but not all the controls
inside it, it cantains textboxes and label etc.

I tryed that:
For Each objForm As Form In Me.MdiChildren
Dim currentFont As Font = objForm.Font
objForm.Font = New Font(currentFont.FontFamily, 10, currentFont.Style, currentFont.Unit)
For Each objControl As Control In objForm.Controls
objControl.Font = New Font(currentFont.FontFamily, 10, currentFont.Style, currentFont.Unit)
If TypeOf objControl Is TextBox Then CType(objControl, TextBox).Font = New Font(currentFont.FontFamily, 6, currentFont.Style, currentFont.Unit)
Next
Next


But its not working :\


How can i do it ?

Thanks.
 
Ok. I wasn't aware that the Control class also has a function for its sub controls. So using recursion we get the following

VB.NET:
    Private Sub ChangeFontSize(ByVal FontSize As Single)
        For Each objForm As Form In Me.MdiChildren
            Dim currentFont As Font = objForm.Font
            objForm.Font = New Font(currentFont.FontFamily, FontSize, currentFont.Style, currentFont.Unit)
            ChangeControlFontSize(FontSize, objForm.Controls)
        Next
    End Sub

    Private Sub ChangeControlFontSize(ByVal FontSize As Single, ByVal Controls As Control.ControlCollection)
        For Each objControl As Control In Controls
            Dim currentFont As Font = objControl.Font
            objControl.Font = New Font(currentFont.FontFamily, FontSize, currentFont.Style, currentFont.Unit)
            ChangeControlFontSize(FontSize, objControl.Controls)
        Next
    End Sub

Now please keep in mind that if you have specialized controls that have different ways to change the font size other than through the font property you'll need to get at it differently.

VB.NET:
            If TypeOf (objControl) Is DataGrid Then
                Dim objDataGrid As DataGrid = CType(objControl, DataGrid)
                'Do objDataGrid work.
            End If
 
Back
Top