How to make bold part of a label?

N

Nicholas Paldino [.NET/C# MVP]

Alexey,

You can create a new Font instance which has the FontStyle.Bold value
set and then set the Font property on the label class to that Font instance.
 
N

Nicholas Paldino [.NET/C# MVP]

Alexey,

You can create a new Font instance which has the FontStyle.Bold value
set and then set the Font property on the label class to that Font instance.
 
J

Jesse Houwing

Hello Alexey,
How to make bold part of the text in a Windows.Forms.Label? Thank you.

If you need one part of the label bold and teh second normal you'd have to
plit them up into two (or more) labels as far as I can tell...

Jesse
 
A

Alexey Smirnov

Alexey,

You can create a new Font instance which has the FontStyle.Bold value
set and then set the Font property on the label class to that Font instance.

In this case the whole text in the label will be bold, I'm right?
 
A

Alexey Smirnov

Hello Alexey,


If you need one part of the label bold and teh second normal you'd have to
plit them up into two (or more) labels as far as I can tell...

Hi Jesse

Actually I would like to know if I can do it in the same label,
without having two or more labels.

Cheers!
 
K

KWienhold

Hi Jesse

Actually I would like to know if I can do it in the same label,
without having two or more labels.

Cheers!

As far as I know it is not possible to use different fonts for
different parts of the same label, since it doesn't support anything
like RTF markups or the like.
So either you use two labels (one for the bold part, one for the
other) or you derive your own label class from label and handle the
drawing yourself.
I guess personally I would go with deriving in this case, since the
implementation would consist mostly of pretty basic GDI+ stuff and is
more flexible than always creating two labels.

hth,
Kevin Wienhold
 
A

Alexey Smirnov

As far as I know it is not possible to use different fonts for
different parts of the same label, since it doesn't support anything
like RTF markups or the like.
So either you use two labels (one for the bold part, one for the
other) or you derive your own label class from label and handle the
drawing yourself.
I guess personally I would go with deriving in this case, since the
implementation would consist mostly of pretty basic GDI+ stuff and is
more flexible than always creating two labels.

hth,
Kevin Wienhold- Hide quoted text -

- Show quoted text -

Okay, thanks

I have overrided the OnPaint() method

For single line text a code would be something like this

// show first 10 letters in bold

e.Graphics.DrawString(this.Text.Substring(0,9),
new System.Drawing.Font("Tahoma", 8.0F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((byte) 0)),
new SolidBrush(this.ForeColor), new
Rectangle(this.Padding.Left, this.Padding.Top, this.Width -
this.Padding.Left, this.Height), StringFormat.GenericDefault);

Size sz = new Size(this.ClientSize.Width, Int32.MaxValue);
sz = TextRenderer.MeasureText(this.Text.Substring(0, 9), this.Font,
sz, TextFormatFlags.WordBreak);

e.Graphics.DrawString(this.Text.Substring(10),
new System.Drawing.Font("Tahoma", 8.0F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((byte)0)),
new SolidBrush(this.ForeColor),
this.Padding.Left + sz.Width, this.Padding.Top);
 
N

Nicholas Paldino [.NET/C# MVP]

Yes. I'm rereading the thread (it didn't seem to me that you wanted
part of the text bold from the original post) and if you don't want two
separate labels, then you will have to create a custom control and paint the
label yourself. You will have to change the font which you want placed in
bold, and draw that particular part in bold when you want to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top