469,354 Members | 2,046 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,354 developers. It's quick & easy.

How to make bold part of a label?

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

Aug 5 '07 #1
8 29628
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
How to make bold part of the text in a Windows.Forms.Label? Thank you.
Aug 5 '07 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
How to make bold part of the text in a Windows.Forms.Label? Thank you.
Aug 5 '07 #3
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
Aug 5 '07 #4
On Aug 5, 9:01 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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?

Aug 6 '07 #5
On Aug 5, 10:38 pm, Jesse Houwing <Jesse.houw...@nospam-sogeti.nl>
wrote:
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...
Hi Jesse

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

Cheers!
Aug 6 '07 #6
On 6 Aug., 09:35, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On Aug 5, 10:38 pm, Jesse Houwing <Jesse.houw...@nospam-sogeti.nl>
wrote:
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...

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

Aug 6 '07 #7
On Aug 6, 12:11 pm, KWienhold <hedov...@trashmail.netwrote:
On 6 Aug., 09:35, Alexey Smirnov <alexey.smir...@gmail.comwrote:


On Aug 5, 10:38 pm, Jesse Houwing <Jesse.houw...@nospam-sogeti.nl>
wrote:
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...
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- 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);

Aug 6 '07 #8
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
On Aug 5, 9:01 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>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?

Aug 6 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by osmarjunior | last post: by
1 post views Thread by chris_huh | last post: by
4 posts views Thread by ravenheart | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.