[英]VB.net VS2015 Change font for entire project
Is there an easy/quick way to change the font for all controls in a VB.net application? 是否有简单/快速的方法来更改VB.net应用程序中所有控件的字体? (Currently using VB 2015)
(当前使用VB 2015)
I mean all text boxes, buttons etc. It should be independent of the systems default font. 我的意思是所有文本框,按钮等。它应该独立于系统默认字体。
Changing each control throughout the entire project (30-40 Forms+) would be extremely labor intensive. 在整个项目中(30-40 Forms +)更改每个控件将非常耗费人力。
Even though I guess its not recommended to change default font family; 即使我认为不建议更改默认字体系列; sometimes its so needed, especially for accessibility, customization and personalization.
有时需要它,尤其是在可访问性,定制和个性化方面。 This also can provide nice function for end user in some other applications.
在某些其他应用程序中,这也可以为最终用户提供不错的功能。
Thanks to many posts and answers and combining them;
感谢许多帖子和答案,并将它们结合在一起; you can try to change entire project's font family during run time.
您可以尝试在运行时更改整个项目的字体系列。
Steps to add custom fonts to VB.NET and Winforms: 将自定义字体添加到VB.NET和Winforms的步骤:
Imports System.Drawing.Text
Public Class Form1
Dim pfc As New PrivateFontCollection()
Private Function FindALLControlRecursive(ByVal list As List(Of Control), ByVal parent As Control) As List(Of Control)
' function that returns all control in a form, parent or child regardless of control's type
If parent Is Nothing Then
Return list
Else
list.Add(parent)
End If
For Each child As Control In parent.Controls
FindALLControlRecursive(list, child)
Next
Return list
End Functio
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
' On Form1 shown, start applying font
Dim CFontPath As String = Application.StartupPath
pfc.AddFontFile(CFontPath & "\Resources\Fonts\Roboto.ttf")
Dim allCtrl As New List(Of Control)
For Each ctrl As Control In FindALLControlRecursive(allCtrl, Me)
' You need to define which control type to change it's font family; not recommendd to just change all controls' fonts, it will create a missy shape
If TypeOf ctrl Is Label Or TypeOf ctrl Is TextBox Or TypeOf ctrl Is Button Or TypeOf ctrl Is CheckBox Or TypeOf ctrl Is RadioButton Or TypeOf ctrl Is ProgressBar Or TypeOf ctrl Is GroupBox Or TypeOf ctrl Is Chart Then
Dim CurrentCtrlFontSize = ctrl.Font.Size ' get current object's font size before applying new font family
ctrl.Font = New Font(pfc.Families(0), CurrentCtrlFontSize, FontStyle.Regular)
End If
Next
allCtrl.Clear()
End Sub
End Class
I can't be sure though this is the optimal code or best approach but it did the job and hope some collaboration. 我不能确定这是最佳的代码还是最佳的方法,但是它确实完成了工作并希望能有所合作。
I know its an old date question but hope this answer helps and its very interesting question. 我知道它是一个过时的问题,但希望这个答案会有所帮助,并且它的问题非常有趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.