Is it possible to format certain text in a WinForm Label instead of breaking the text into multiple labels? Please disregard the HTML tags within the label's text; it's only used to get my point out.
For example:
Dim myLabel As New Label
myLabel.Text = "This is <b>bold</b> text. This is <i>italicized</i> text."
Which would produce the text in the label as:
This is bold text. This is italicized text.
Worked solution for me - using custom RichEditBox. With right properties it will be looked as simple label with bold support.
1) First, add your custom RichTextLabel class with disabled caret :
2) Split you sentence to words with IsSelected flag, that determine if that word should be bold or no :
3) Add function for convert you text to valid rtf (with unicode support!) :
Works like a charm for me! Solutions compiled from :
How to convert a string to RTF in C#?
Format text in Rich Text Box
How to hide the caret in a RichTextBox?
In the Form1_load do
myRtfControl.Rtf = Resource1.MyRtfControlText
AutoRichLabel
I was solving this problem by building an
UserControl
that contains aTransparentRichTextBox
that is readonly. TheTransparentRichTextBox
is aRichTextBox
that allows to be transparent:TransparentRichTextBox.cs:
The final
UserControl
acts as wrapper of theTransparentRichTextBox
. Unfortunately, I had to limit it toAutoSize
on my own way, because theAutoSize
of theRichTextBox
became broken.AutoRichLabel.designer.cs:
AutoRichLabel.cs:
The syntax of the rich text format is quite simple:
Paragraph:
Bold / Italic / Underline text:
Alternate color using color table:
But please note: Always wrap every text in a paragraph. Also, different tags can be stacked (i.e.
\pard\b\i Bold and Italic\i0\b0\par
) and the space character behind a tag is ignored. So if you need a space behind it, insert two spaces (i.e.\pard The word \bBOLD\0 is bold.\par
). To escape\
or{
or}
, please use a leading\
. For more information there is a full specification of the rich text format online.Using this quite simple syntax you can produce something like you can see in the first image. The rich text content that was attached to the
RtfContent
property of myAutoRichLabel
in the first image was:If you want to enable word wrap, please set the maximum width to a desired size. However, this will fix the width to the maximum width, even when the text is shorter.
Have fun!
Not really, but you could fake it with a read-only RichTextBox without borders. RichTextBox supports Rich Text Format (rtf).
Another workaround, late to the party: if you don't want to use a third party control, and you're just looking to call attention to some of the text in your label, and you're ok with underlines, you can use a LinkLabel.
Note that many consider this a 'usability crime', but if you're not designing something for end user consumption then it may be something you're prepared to have on your conscience.
The trick is to add disabled links to the parts of your text that you want underlined, and then globally set the link colors to match the rest of the label. You can set almost all the necessary properties at design-time apart from the
Links.Add()
piece, but here they are in code:Result:
A FlowLayoutPanel works well for your problem. If you add labels to the flow panel and format each label's font and margin properties, then you can have different font styles. Pretty quick and easy solution to get working.