How does VB. NET zhangyuge use the external text format in the project?

Source: Internet
Author: User

How does VB. NET zhangyuge use the external text format in the project?
When we set the control font, we sometimes find that many fonts cannot be found in the VS editor. For example, the Times New Roman font is commonly used, but cannot be found in the editor. If you do not believe it, try it.

First, you need to find the file in the format of Times New Roman. This can be downloaded from the Internet or directly searched on the hard disk of your computer. Generally, all files in the computer are saved in the text format. stored in ttf format.
Open the computer and enter "?. The ttf search may take some time. The specific search example is as follows:

Step 2: copy the Times New Roman format file to the folder of the current project. I created a res folder at the layer where the Sln file of the file is located, copy the format file to the folder. (You can store it in your own way, as long as you modify the code)

Step 3: In the code editor, write an ImportFont () process with the returned value. The Code is as follows:

Imports System. drawing. textModule Module2 'import the external Font Public Function ImportFont () As font' to obtain the Path of the Font file Dim Path = Replace (Application. startupPath, "\ bin \ Debug", "\ res \ segoeui. ttf ") Try Dim NewFont1 As New PrivateFontCollection 'create a personal format collection object NewFont1.AddFontFile (Path) 'access the format file through the Path (the Path here must be correct)' New Font () multiple parameters are available. The first parameter is the format name, the second parameter is the font size, and the third parameter is the font width, positive and oblique underline options, 'The fourth parameter is the unit of measurement of the font, followed by the optional parameter. You can try Dim NewFont As Font = New Font (NewFont1.Families (0) on your own ). name, 12, FontStyle. regular, GraphicsUnit. pixel) Return NewFont Catch ex As Exception Return Nothing End Try End FunctionEnd Module


In this way, you can call this function to obtain a variable of the Font type. The value of this variable is in The Times NewRoman format.
'Set the Font Private Sub SetControlFont () Dim Font1 As Font = ImportFont () 'of the control in the entire form to use the nested loop statement, make sure that the font of Each Control is set If Font1 IsNot Nothing Then For Each Cotr As Control In Controls Cotr. font = Font1 If Cotr. controls IsNot Nothing Then For Each Cotr1 As Control In Cotr. controls Cotr1.Font = Font1 Next End If End Sub
Okay. After setting, you can call the SetControlFont () function in the form Load event to set the font of each control on the project form to Times New Roman.

 Private Sub frm_Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        SetControlFont()    End Sub


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.