Formatting text in WinForm Label

2019-01-03 17:03发布

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.

12条回答
该账号已被封号
2楼-- · 2019-01-03 17:09

There is an excellent article from 2009 on Code Project named "A Professional HTML Renderer You Will Use" which implements something similar to what the original poster wants.

I use it successfully within several projects of us.

查看更多
虎瘦雄心在
3楼-- · 2019-01-03 17:09

Very simple solution:

  1. Add 2 labels on the form, LabelA and LabelB
  2. Go to properties for LabelA and dock it to left.
  3. Go to properties for LabelB and dock it to left as well.
  4. Set Font to bold for LabelA .

Now the LabelB will shift depending on length of text of LabelA.

That's all.

查看更多
男人必须洒脱
4楼-- · 2019-01-03 17:10

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.
查看更多
姐就是有狂的资本
5楼-- · 2019-01-03 17:15

Realising this is an old question, my answer is more for those, like me, who still may be looking for such solutions and stumble upon this question.

Apart from what was already mentioned, DevExpress's LabelControl is a label that supports this behaviour - demo here. Alas, it is part of a paid library.

If you're looking for free solutions, I believe HTML Renderer is the next best thing.

查看更多
叼着烟拽天下
6楼-- · 2019-01-03 17:22

Yeah. You can implements, using HTML Render. For you see, click on the link: https://htmlrenderer.codeplex.com/ I hope this is useful.

查看更多
姐就是有狂的资本
7楼-- · 2019-01-03 17:25

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.

查看更多
登录 后发表回答