Click here to Skip to main content
15,885,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

How could I change the label text to bold when events raise eg: On button click, the label will be bold? I have search in Internet and try but nothing work out.I already imports this:

Imports System.Drawing.Font
Imports System.Windows.Forms
Imports System.Windows.Media.Fonts

This is what I've add but it show blue string. This is my code "lblBarcode.Font = New Fonts With {.FontStyle = Bold}".

At "New Fonts" is show that "Types 'System.Windows.Media.Fonts' has no constructor"

I also try this code but it's also not work:

Dim newFont as New Font(lblBarcode.Font, FontStyle.Bold)
lblBarcode.Font = newFont
Posted
Updated 16-Apr-12 4:02am
v2

I'm assuming your program is a Winforms application. Otherwise, stop reading.
I won't give you a solution, but some advice:

  1. Put a label you don't use in your form.
  2. In the properties grid, change its font (typeface, color, weight, size, everything).
  3. Assuming your form is called "Form1", open the file "Form1.Designer.vb"
  4. Browse to the corresponding line, and read how it's done
  5. Copy, paste, edit...


I got this:

VB
'myLabel
'
Me.myLabel.Font = New System.Drawing.Font("Lucida Handwriting", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(177, Byte))
Me.myLabel.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(192, Byte), Integer))


Note that the designer uses System.Drawing.Font, not System.Windows.Media.Fonts.

Hope this helps,
Pablo.
 
Share this answer
 
Comments
VJ Reddy 16-Apr-12 11:06am    
Good advice. 5!
Luiey Ichigo 16-Apr-12 20:56pm    
Which mean this is the designer property code for every toolbox item? I understand your explanation..thank you Pablo
Luiey Ichigo 16-Apr-12 21:05pm    
I already use your steps..it's work.TQ pablo..5
The advice given by Pablo Aliskevicius in Solution 1 is very good.
I want to add the following
Create two Font objects, one with Bold style and other with Regular style like
VB
Dim BoldFont As New Font("Arial", 12, FontStyle.Bold)
Dim RegularFont As New Font("Arial", 12, FontStyle.Regular)
'To make the Font of Label1 as Bold, in the click event of a Button set 
Label1.Font = BoldFont
'And to change the Font to Regular font, set the following in the Click event of 'another button
Label1.Font = RegularFont
 
Share this answer
 
Comments
Kuldeep B 16-Apr-12 12:12pm    
Perfect Answer VJ
VJ Reddy 16-Apr-12 12:38pm    
Thank you, Kuldeep.
Luiey Ichigo 16-Apr-12 21:11pm    
Tq VJ for your solution. Both of you and Pablo solution works for me.I give u 5! Thanks
VJ Reddy 16-Apr-12 22:15pm    
You're welcome and thank you for your vote.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900