|
-
Nov 26th, 2011, 03:28 AM
#1
Thread Starter
Junior Member
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.
-
Nov 26th, 2011, 04:31 AM
#2
Thread Starter
Junior Member
Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?
-
Nov 26th, 2011, 03:59 PM
#3
Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?
Something like this:-
vb Code:
Private Sub ChangeFont(ByVal F As Font) For Each C As Control In Me.Controls C.Font = F Next 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:
Private Sub ChangeFont(ByVal ContainerCtl As Control, ByVal F As Font) For Each C As Control In ContainerCtl.Controls C.Font = F Next End Sub
ContainerCtl would be the object where all the objects who's fonts you wish to change are situated.
-
Nov 26th, 2011, 04:47 PM
#4
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
-
Nov 26th, 2011, 04:53 PM
#5
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
-
Dec 1st, 2011, 11:13 PM
#6
Lively Member
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... 
-
Dec 2nd, 2011, 03:37 AM
#7
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.
-
Dec 2nd, 2011, 03:51 AM
#8
Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?
 Originally Posted by kevininstructor
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.
-
Dec 2nd, 2011, 03:53 AM
#9
Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?
 Originally Posted by happycamper1221
still waiting? :S
PS: I think 1 day is enough to wait before bumping . 1 hour... maybe not.
-
Dec 2nd, 2011, 11:44 AM
#10
Re: how do i Change font/text size of every "label,button,groupbox" ect. Globally?
 Originally Posted by Grimfort
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|