tags:

views:

510

answers:

7

Hi, is there any way to make a part of a label.text Bold?

label.text = "asd" + string;

string should be bold :)

+2  A: 

WinForms don't allow to do it.

iburlakov
it's not specially easy, but possible.
serhio
Agree with you, serhio.
iburlakov
+1  A: 

In WinForms override Label.OnPaint() method and draw the text your self.

Tadas
however, if you set `label.text = "asd" + string;` I don't know how you will separate **bold** from regular.
serhio
+1  A: 

In ASP.NET you could do:

label.Text = string.Format("asd <span style='font-weight: bold;'>{0}</span>", string);

But like everyone else says, depends on what you're using.

Town
+3  A: 

WebForms

Use Literal control, and add a <b> tag around the part of the text you want:

_myLiteral.Text = "Hello <b>big</b> world";

Winforms

Two options:

  1. Put two labels side by side (far easier)
  2. Subclass Label and do your own custom drawing in the OnPaint() method.

The second choice has been answered already.

Chris S
+2  A: 

Hi,

The easy way to do what you want is just to add two labels. In this way you could make one bold, and it will look ok with a proper positioning.

The normal way would be to create a control that has two or more labels and you could set the properties on each one of them. Also this has the advantage that is reusable.

Adrian Faciu
+1  A: 

Does it need to be a Label control, or do you just need to put text in a particular place? If the former, you'll need to do custom painting as other people have noted. If not, you could use a readonly RichTextBox instead.

Simon
+5  A: 

The following class illustrate how to do it using overriding OnPaint() in Lable class of WindowsForm. You can refine it. But what i did is that i use pipe '|' in a string to tell on paint function to print text before the | as bold and after it as normal text.

class LabelX : Label
{
    protected override void OnPaint(PaintEventArgs e) {
        Point drawPoint = new Point(0, 0);

        string[] ary = Text.Split(new char[] { '|' });
        if (ary.Length == 2) {
            Font normalFont = this.Font;

            Font boldFont = new Font(normalFont, FontStyle.Bold);

            Size boldSize = TextRenderer.MeasureText(ary[0], boldFont);
            Size normalSize = TextRenderer.MeasureText(ary[1], normalFont);

            Rectangle boldRect = new Rectangle(drawPoint, boldSize);
            Rectangle normalRect = new Rectangle(
                boldRect.Right, boldRect.Top, normalSize.Width, normalSize.Height);

            TextRenderer.DrawText(e.Graphics, ary[0], boldFont, boldRect, ForeColor);
            TextRenderer.DrawText(e.Graphics, ary[1], normalFont, normalRect, ForeColor);
        }
        else {

            TextRenderer.DrawText(e.Graphics, Text, Font, drawPoint, ForeColor);                
        }

Here how it is down

        LabelX x = new LabelX();
        Controls.Add(x);
        x.Dock = DockStyle.Top;
        x.Text = "Hello | World";       

Hello will be printed in bold and world in normal.

affan
This is off course for .NET Window Forms.
affan