Diffrent fonts in one label

R

Roggey

Hello

I'm using System.Windows.Forms.Label and I want to add some text, where
one or more words are bold or italic.

I tried it with html tags but does not work. Somewhere i did read it
should be done by html tags. I did it like this:
label1.Text="my <b>test</b> string";
In the output i can see the html tags.<

I hope someone can tell me how to do this.

Thanks

Roggey
 
B

Bjorn Abelli

Not with the regular Label...

You have possibly seen that mentioned for the Java JLabel...

You can construct a Control of your own.

To give you a hint what you can do, here's a speedy hack, inheriting from a
RichTextBox:


class RTFLabel : System.Windows.Forms.RichTextBox
{
public RTFLabel()
{
// Note! This control can't take "Transparent" as BackColor,
// So you'll have to make something of this...

this.BackColor = System.Drawing.SystemColors.InactiveBorder;
this.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.ReadOnly = true;
}

public void Append(string text, Font f)
{
int length = text.Length;
int start = this.Text.Length;
this.AppendText(text);
this.Select(start, length);
this.SelectionFont = f;
this.Select(0,0);
}

public void Append(string text, FontStyle fs)
{
Font f = new Font(this.Font, fs);
this.Append(text, f);
}

public void AppendRegular(string text)
{
this.Append(text, FontStyle.Regular);
}
public void AppendBold(string text)
{
this.Append(text, FontStyle.Bold);
}
public void AppendItalic(string text)
{
this.Append(text, FontStyle.Italic);
}
public void AppendUnderline(string text)
{
this.Append(text, FontStyle.Underline);
}

public void AppendStrikeout(string text)
{
this.Append(text, FontStyle.Strikeout);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top