Reputation: 4435
When you create a Label
in WinForms, the ForeColor
is ControlText
and the BackColor
is Control
, which produce this kind of Label
:
I want to be able to set a different ForeColor
, different BackColor
and different Font
(bold) to some of the words inside a label. Something like this:
I googled it but all I have found is answers about changing the entire label style. So how can I accomplish what I described?
If there isn't a simple way to do it using built-in C# stuff, how to approach this?
Upvotes: 1
Views: 750
Reputation: 8194
I'd agree with ChrisF that a readonly RichTextBox is best suited for this purpose.
Here's an example of a readonly RichTextBox control that I've used in the past.
public class DisabledRichTextBox : RichTextBox
{
private const int WmSetfocus = 0x07;
private const int WmEnable = 0x0A;
private const int WmSetcursor = 0x20;
protected override void WndProc(ref Message m)
{
if (!(m.Msg == WmSetfocus || m.Msg == WmEnable || m.Msg == WmSetcursor))
{
base.WndProc(ref m);
}
}
}
To use the code:
Add a new class to your project. Personally I'd add a new .cs
file called something like DisabledRichTextBox.cs
. Paste this code in between the namespace
tag:
using whatever;
namespace YourNamespace
{
// Code here
}
Build your project as normal.
DisabledRichTextBox
or whatever you called it. .Rtf
(richtext) property of this new RichTextBox to some appropriate RichText.Upvotes: 2