简体   繁体   English

VB.net VS2015更改整个项目的字体

[英]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 +)更改每个控件将非常耗费人力。

vb.net custom font vb.net自定义字体

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. 您可以尝试在运行时更改整个项目的字体系列。

  • This is tested in VS 2017 CE , VB.Net 4.6.1; 这已在VS 2017 CE,VB.Net 4.6.1中进行了测试; can't be sure for VS 2015. 无法确定VS 2015。
  • This answer also includes at the same time applying google fonts without installing them into end user's OS 此答案还包括同时应用Google字体而不将其安装到最终用户的操作系统中

Steps to add custom fonts to VB.NET and Winforms: 将自定义字体添加到VB.NET和Winforms的步骤:

  1. Grape a font from Google Fonts (Download as TTF). Grape是Google字体中的一种字体(下载为TTF)。
  2. Add the font into your solution. 将字体添加到您的解决方案。
  3. Reference the new custom font and load it as an object. 引用新的自定义字体并将其作为对象加载。
  4. Reference all controls in a form. 以表格形式引用所有控件。
  5. Check if the control accept/need font customization. 检查控件是否接受/需要字体自定义。
  6. Apply the new font family. 应用新的字体系列。
  7. Run and test. 运行并测试。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM