Results 1 to 10 of 10

Thread: how do i Change font/text size of every "label,button,groupbox" ect. Globally?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Red face how do i Change font/text size of every "label,button,groupbox" ect. Globally?

    Hi, I have a checkbox in my options form to change every every font size in the form to something like "20" when it's checked. is there a way to globally change the font size of everything when the checkboxes checkstate becomes checked. Thank you.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    29

    Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?

    still waiting? :S

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?

    Something like this:-
    vb Code:
    1. Private Sub ChangeFont(ByVal F As Font)
    2.  
    3.         For Each C As Control In Me.Controls
    4.             C.Font = F
    5.         Next
    6.  
    7.     End Sub
    That function would work on forms, usercontrols and controls. If you plan on putting a function like that into a module or any class that is not a container you could do it like this:-
    vb Code:
    1. Private Sub ChangeFont(ByVal ContainerCtl As Control, ByVal F As Font)
    2.  
    3.         For Each C As Control In ContainerCtl.Controls
    4.             C.Font = F
    5.         Next
    6.  
    7.     End Sub
    ContainerCtl would be the object where all the objects who's fonts you wish to change are situated.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?

    Code:
        Private Sub CheckBox1_CheckedChanged(sender As System.Object, _
                                             e As System.EventArgs) Handles CheckBox1.CheckedChanged
            If CheckBox1.Checked Then
                Dim ctrl As Control = Me.GetNextControl(Me, True) 'thanks jmc
                Do Until ctrl Is Nothing
                    Dim f As Font = New Font(ctrl.Font.FontFamily, 20.0F, ctrl.Font.Style)
                    ctrl.Font = f
                    ctrl = Me.GetNextControl(ctrl, True)
                Loop
            End If
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?

    Figure out the base control i.e. your form for instance then data bind all desired controls to your form i.e.

    In the cmdChangeFont click event we assign a font to the form and the three controls in form load will use the new font.

    Code:
        Private Sub Form1_Load( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
    
            Button1.DataBindings.Add("Font", Me, "Font")
            Label1.DataBindings.Add("Font", Me, "Font")
            CheckBox1.DataBindings.Add("Font", Me, "Font")
        End Sub
        Private Sub cmdChangeFont_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles cmdChangeFont.Click
    
            If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.Font = FontDialog1.Font
            End If
        End Sub
    End Class

  6. #6
    Lively Member CoderKid's Avatar
    Join Date
    Oct 2011
    Location
    In the middle of nowhere
    Posts
    72

    Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?

    I think this can do it:

    Code:
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
            If CheckBox1.Checked = True Then
                Label1.Font = New Font("Consolas", 20, FontStyle.Regular)
                Label2.Font = New Font("Consolas", 20, FontStyle.Regular)
                Label3.Font = New Font("Consolas", 20, FontStyle.Regular)
                Label4.Font = New Font("Consolas", 20, FontStyle.Regular)
                Label5.Font = New Font("Consolas", 20, FontStyle.Regular)
                Label6.Font = New Font("Consolas", 20, FontStyle.Regular)
                Label7.Font = New Font("Consolas", 20, FontStyle.Regular)
            Else
                Label1.Font = New Font("Consolas", 10, FontStyle.Regular)
                Label2.Font = New Font("Consolas", 10, FontStyle.Regular)
                Label3.Font = New Font("Consolas", 10, FontStyle.Regular)
                Label4.Font = New Font("Consolas", 10, FontStyle.Regular)
                Label5.Font = New Font("Consolas", 10, FontStyle.Regular)
                Label6.Font = New Font("Consolas", 10, FontStyle.Regular)
                Label7.Font = New Font("Consolas", 10, FontStyle.Regular)
            End If
    
        End Sub
    I know it's pretty long, but I think that it is pretty good right now, because I'm dead tired! Hope it works!
    <-- If you like my post, click on the to the left! It only takes a second...

  7. #7
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?

    Here is how I do it:

    Code:
    Friend Sub FormControl_ApplyApplicationFont(ByVal ControlToApply As Control, Optional ByVal Recursive As Boolean = True, Optional ByVal ContainerCheck As Boolean = False)
    
        Try
            ApplyApplicationFont(ControlToApply)
            If Recursive AndAlso ControlToApply.HasChildren Then
                For Each objControl As Control In ControlToApply.Controls
                    FormControl_ApplyApplicationFont(objControl, True)
                Next
            End If
        Catch ex As Exception
            'Do not throw up errors when changing font, the existing font will be left alone
        End Try
    
    End Sub
    
    Private Sub ApplyApplicationFont(ByVal ControlToApply As Control)
    
        'Do not apply the application font to Menus and Toolbars as they choose a different systemfont property
        'i.e. System.Drawing.SystemFonts.MenuFont and System.Drawing.SystemFonts.CaptionFont etc
        '	Return
    
        'Exclusion list example
        'Select Case True
        '	Case TypeOf ControlToApply Is MenuItem
        '		Return
        'End Select
    
        Try
            ControlToApply.Font = FormControl_CreateApplicationFontProperty(ControlToApply.Font)
        Catch ex As Exception
    #If DEBUG Then
            MessageBox.Show("New control that does not allow font to be changed. Add this to the excludes list in the ApplyApplicationFont method")
    #End If
        End Try
    
        'I have a custom font for some 3rd party controls
        'Controls that require secondary fonts to be set
        Select Case True
            Case TypeOf ControlToApply Is xxx
    
        End Select
    End Sub
    
    Friend Function FormControl_CreateApplicationFontProperty(ByVal FontToBaseUpon As Font, Optional ByVal FontSize As Single = 0) As Font
    
        If FontToBaseUpon Is Nothing Then
            FontToBaseUpon = System.Drawing.SystemFonts.DefaultFont
        End If
        With FontToBaseUpon
            If FontSize > 0 Then
                Return New Font(ApplicationFontFamily, FontSize, .Style, .Unit)
            Else
                Return New Font(ApplicationFontFamily, .Size, .Style, .Unit)
            End If
        End With
    End Function
    I can call the first method with a form on load like so:

    Code:
    FormControl_ApplyApplicationFont(Me)
    or I can call in a loop for all forms at any time:

    Code:
    For Each frmForm As Form In Application.OpenForms
         FormControl_ApplyApplicationFont(frmForm)
    Next

    To set the current font, I have a global variable (used above) that I can just change when I need to:

    Code:
    Friend ApplicationFontFamily As FontFamily = System.Drawing.SystemFonts.IconTitleFont.FontFamily

    The reason for all the extra code is to ensure that when the font is changed, only the family is changed, the size, bold, italic etc are all kept as they have been designed to be this format. Possibly you do not need this now, but later you will .
    Last edited by Grimfort; Dec 2nd, 2011 at 03:48 AM.

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?

    Quote Originally Posted by kevininstructor View Post
    Figure out the base control i.e. your form for instance then data bind all desired controls to your form i.e.
    I have never actually seen a decent reason to learn what the binding properties are for, now I have seen one! (I learn by example mainly). Bit long a process for this example, but great little tool to have in the bag. Ta.

  9. #9
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?

    Quote Originally Posted by happycamper1221 View Post
    still waiting? :S

    PS: I think 1 day is enough to wait before bumping . 1 hour... maybe not.

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?

    Quote Originally Posted by Grimfort View Post
    Here is how I do it:
    To set the current font, I have a global variable (used above) that I can just change when I need to:

    Code:
    Friend ApplicationFontFamily As FontFamily = System.Drawing.SystemFonts.IconTitleFont.FontFamily

    The reason for all the extra code is to ensure that when the font is changed, only the family is changed, the size, bold, italic etc are all kept as they have been designed to be this format. Possibly you do not need this now, but later you will .
    Nicely done, especially in regards to the font family.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width