views:

1854

answers:

4

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.

+1  A: 

I Would also be interested in finding out if it is possible.

When we couldn't find a solution we resorted to Component Ones 'SuperLabel' control which allows HTML markup in a label.

Martin
+9  A: 

That's not possible with a WinForms label as it is. The label has to have exactly one font, with exactly one size and one face. You have a couple of options:

  1. Use separate labels
  2. Create a new Control-derived class that does its own drawing via GDI+ and use that instead of Label; this is probably your best option, as it gives you complete control over how to instruct the control to format its text
  3. Use a third-party label control that will let you insert HTML snippets (there are a bunch - check CodeProject); this would be someone else's implementation of #2.
DannySmurf
+3  A: 

Not really, but you could fake it with a read-only RichTextBox without borders. RichTextBox supports Rich Text Format (rtf).

ageektrapped
+1  A: 
  1. Create the text as a RTF file in wordpad
  2. Create Rich text control with no borders and editable = false
  3. Add the RTF file to the project as a resource
  4. In the Form1_load do

    myRtfControl.Rtf = Resource1.MyRtfControlText

Phil