Using custom fonts on a Label on Winforms
I have a label on my Winform and I want to use a custom font called XCalibur to make it appear more shnazzy.
If I use a custom font on a label and then build the solution and then .ZIP the files in \bin\Release will the end user see the labels with my custom app I used regardless if they have that font installed or not?
If this isn’t the case, what’s the proper way to use Custom Fonts on Labels.Text?
4 Answers 4
Embed the font as a resource (or just include it in the bin directory), and then use the PrivateFontCollection to load the font (See the AddFontFile and AddMemoryFont functions). You then use the font normally like it was installed on the machine.
The PrivateFontCollection class allows applications to install a private version of an existing font without the requirement to replace the system version of the font. For example, GDI+ can create a private version of the Arial font in addition to the Arial font that the system uses. PrivateFontCollection can also be used to install fonts that do not exist in the operating system.
After looking through possibly 30-50 posts on this, I have finally been able to come up with a solution that actually works! Please follow the steps sequentially:
1.) Include your font file (in my case, ttf file) in your application resources. To do this, double-click on the «Resources.resx» file.
2.) Highlight the «Add resource» option and click the down-arrow. Select «Add existing file» option. Now, search out your font file, select it, and click OK. Save the «Resources.resx» file.
3.) Create a function (say, InitCustomLabelFont() ), and add the following code in it.
Your custom font has now been added to the PrivateFontCollection.
4.) Next, assign the font to your Label, and add some default text into it.
5.) Go to your form layout and select your label. Right-click it and select «Properties«. Look for the property «UseCompatibleTextRendering» and set it to «True«.
6.) If necessary you can release the font after you are sure that it can never be used again. Call the PrivateFontCollection.Dispose() method, you can then also call Marshal.FreeCoTaskMem(data) safely. It is pretty common to not bother and leave the font loaded for the life of the app.
7.) Run your application. You shall now see that you custom font has been set for the given label.
Windows form label font
This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.
Answered by:
Question
I just want to change the color of the font based on a condition. If chk.checked = true Then
label.text.font = RED. Something like that. But I can’t find the right way to call that. Writing in vb.net.
Answers
With your checkbox named as chk and the label named as Label ( in the same way you have ) then use this code please.>>
Public Class Form1
‘The next h ighlighted line should be on one line in your code window.>>>>
Private Sub Chk_CheckedChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chk.CheckedChanged
If Chk.Checked = True Then
‘Choose the Color you want when it is unchecked.>>
All replies
Use the CheckChanged event of the controlling checkbox (you can double click the checkbox on the form designer and the empty event code will be auto-generated).
Private Sub CheckBox1_CheckedChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Thanks for the reply. Will ForeColor change the text color? I thought I tried that, and it didn’t change it. I’ll try it again tonite and check it out. Maybe I’m just missing something.
With your checkbox named as chk and the label named as Label ( in the same way you have ) then use this code please.>>
Public Class Form1
‘The next h ighlighted line should be on one line in your code window.>>>>
Private Sub Chk_CheckedChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chk.CheckedChanged
If Chk.Checked = True Then
‘Choose the Color you want when it is unchecked.>>
Thanks for the reply. That changes the fore color, but I need to change the font color. The fore color will just fill in my textbox red, not the actual text. How do you think I should change the text color?
Thanks for the reply. That changes the fore color, but I need to change the font color. The fore color will just fill in my textbox red, not the actual text. How do you think I should change the text color?
Please read the Visual Basic 2005 stuff on this page.>>
The only way I can see of changing the color is to change the ForeColor.
If you want to change the color when you draw a string then see this page which shows the following code example.
Which version of VB or VB.Net or Visual Studio or Express Edition are you using please?
In VB.Net ForeColor property changes the text color on a label in the version I am using , the BackColor changes the Background color.
The ForeColor in the next line is Red and the BackColor is Black.
Example text.
Really the ForeColor should be changing your LABEL text color.
Putting the above code into a PictureBox paint event you get the following but this is using the DrawString method within the PictureBox1.Paint event and creating the PictureBox at run-time too.
PASTE all of this into an empty code window please.>>
Public Class Form1
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
‘The next highlighted line should be on one line in your code window.>>>>
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .Load
Me .PictureBox1 = New System.Windows.Forms.PictureBox
CType ( Me .PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me .PictureBox1.BackColor = System.Drawing.Color.White
Me .PictureBox1.Location = New System.Drawing.Point(12, 12)
Me .PictureBox1.Name = «PictureBox1»
Me .PictureBox1.Size = New System.Drawing.Size(630, 242)
Me .PictureBox1.TabIndex = 0
Me .PictureBox1.TabStop = False
Me .AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me .ClientSize = New System.Drawing.Size(654, 285)
Me .Controls.Add( Me .PictureBox1)
CType ( Me .PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me .ResumeLayout( False )
‘The next highlighted line should be on one line in your code window.>>>>
Private Sub PictureBox1_Paint( ByVal sender As Object , ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim slateBlue As Color = Color.FromName( «SlateBlue» )
Dim g As Byte = slateBlue.G
Dim b As Byte = slateBlue.B
Dim r As Byte = slateBlue.R
Dim a As Byte = slateBlue.A
‘The next highlighted line should be on one line in your code window.>>>>
Dim text As String = String .Format( «Slate Blue has these ARGB values: Alpha:<0>, » & «red:<1>, green: <2>, blue <3>» , New Object () )
‘The next highlighted line should be on one line in your code window.>>>>
e.Graphics.DrawString(text, New Font( Me .Font, FontStyle.Italic), New SolidBrush(slateBlue), New RectangleF( New PointF(0.0F, 0.0F), Size.op_Implicit( Me .Size)))
I can NOT see anything in the NEW methods for a FONT declaration that will let you change the color of the text, sorry.
Try typing this line.>>
Dim font1 As New Font( Me .Font, myFontStyle)
when you get to the first bracket the » ( «
( then hit the escape key ) and then look at the 13 overloaded versions for a NEW font declaration using the up and down arrow keys.
That is what helped me to type in the following code, whether it works is another matter.
Dim myFontFamily As New FontFamily( «Arial» )
Dim myFontSize As Single = 10.0!
Dim myFontStyle As FontStyle = FontStyle.Bold ‘Could be Italic or Regular or StrikeOut or UnderLine
Dim myGDICharSet As Byte = Convert.ToByte( «23» )
Dim myGdiVerticalFont As Boolean = True
Dim font1 As New Font( Me .Font, myFontStyle)
Dim font2 As New Font(myFontFamily, myFontSize, myFontStyle, GraphicsUnit.Pixel, myGDICharSet, myGdiVerticalFont)
A load of info! Thanks!! I’ll take a look at it today and let you know. I’m using VS 2005/vb.net. Seems a little diffrent than 2003. One thing I noticed is I’m using a textbox and not a label. I didn’t even think about it before until this morning. That might make a diffrence. I’ll try your suggestions and let you know.
Moving this problem into another direction, I want to do the same thing. Change the text color based on a If Then statement. Same one actually. But this is on a data grid. I have done it for ASP.net, but never a windows application.
My first attempt is this,
Private Sub grdMain_CellFormatting( ByVal sender As Object , ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles grdMain.CellFormatting
If e.RowIndex > 0 And e.ColumnIndex = Me .grdMain.Columns(6).Index Then
If Not e.Value Is Nothing Then
Dim Stat As CheckState
If Stat = CheckState.Checked Then
Me .grdMain.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Red
Me .grdMain.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Black
If you don’t want to address the second issue, that’s fine. I normally don’t ask two questions in the same post. But they are so close to the same problem.
Thanks again for all you help! I’ll let you know later today!
I tried all of your ideas, and they all worked. I worked on my code again and did some simple test.
on the Load event. Worked great! Than I put that in with a if then statement, and it gave me a red box. How strange?
So I started over again, just with the first statement. Worked fine. I slowly built my code, testing everytime, and it seems to work. Here is my final code
If StatCheckBox.Checked = True Then
Another thing I noticed was I had my event on the «Checked_changed» event, I should have had on the load event.
Now I just need to figure out the datagrid problem.
Thanks for all your help!
If StatCheckBox.Checked = True Then
Thanks for all your help!
If the above textboxes are all of your textboxes you can do this too.>>
Public Class Form1
‘The next highlighted line should be one line of code in your code window.>>>>
Private Sub StatCheckBox_CheckedChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatCheckBox.CheckedChanged
If StatCheckBox.Checked = True Then
‘Loops through every CONTROL on the FORM. 🙂 >>
For Each ctrl As Control In Me .Controls
‘Is it a TextBox?
If TypeOf (ctrl) Is TextBox Then
‘Loops through every CONTROL on the FORM. 🙂 >>
For Each ctrl As Control In Me .Controls
‘Is it a TextBox?
If TypeOf (ctrl) Is TextBox Then
Thanks for the tip! I have more textboxes that wouldn’t apply. I’ll keep that in mind for next time.
Thanks for all you help!
I am currently working on an excel spreadsheet with forms that input the information. I have all the inputting forms how I want with the features I want but now I want to create a search form that can find either a name or policy number and depending on what tab you have selected it will search that certain sheet in my database. The problem I am currently running into is that my sheets are color coded and therefore I have a blank label that when the certain tab is clicked that label turns to the corresponding color. This is what I have so far and it is telling me that my qualifier is invalid. Thanks for any help.
Private Sub TabStrip1_Click(ByVal Index As Long)
If TabStrip1.SelectedItem.Index = 1 Then
lbltabchecker.BackColor = Color.Green
Else
If TabStrip1.SelectedItem.Index = 2 Then
lbltabchecker.BackColor = Color.Red
Else
If TabStrip1.SelectedItem.Index = 3 Then
lbltabchecker.BackColor = Color.Gold
Else
lbltabchecker.BackColor = SystemColors.ControlText
End If
. Pretty basic coding since I haven’t coded since 04 so please improve at will. Thanks