-3

I want to display the text2 as bold font

   public partial class Form2 : Form
   {
        public Form2(string text1, string text2, string text3, double text4,string t)
        {
           InitializeComponent();
           label1.Text = text1;
           label2.Text = "On  " + text2 +
                         "  Or When Your Vehicle Runs  " + text3 + " KM. ";
           label3.Text ="Today "+ t +
                        "  and It Reminds You " + text4 +" Days Earlir.";
        }
   }
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
user3399062
  • 21
  • 1
  • 5

3 Answers3

1

You can't do it directly on Windows forms .

Try like this

http://www.codeproject.com/Articles/31237/GMarkupLabel-A-C-Windows-Forms-control-to-display

Ondipuli
  • 468
  • 3
  • 9
1

Here is a lightweight Label class that supports emphasized test.

Edit : After visiting CodeReview I have updated my answer. It is much more robust now. It still uses a Label subclass. You can create strong text like this:

This is *bold* and this is an \* asterisk.

for your question you would write:

label2.Text = "On  *" + text2 + "*  Or When Your Vehicle Runs  " + text3 + " KM. ";

Just add the class to your project, adapt its namespace, compile, drag from the toolbox and change its extended properties like a plain Label.

You can set both a StrongColor and a StrongFont to whatever you like. Default is ControlText and the Label.FontStyle.Bold.

You can change the formatting string Splitterfrom the asterisk (*) to anthing and you can escape it by a '\' or whatever Escape string you want, as both are exposed as Properties.

The FmtLabel will autosize its Width and is meant for one line of text only.

You may want to have a look at an expanded version here on CodeReview. At under 200 lines of code it is still rather small, will support multiple lines, up to 10 fonts and even multiple interactive links. (Updated Version 2 soon to come)

using System;
using System.Drawing;
using System.Windows.Forms;

namespace yourNamespace
{
  public class FmtLabel : Label
  {
    public FmtLabel()
    {
        this.Paint += FmtLabel_Paint;
        Escape = @"\";
        Splitter = "*";
        AutoSize = true;
    }

    public string Splitter    { get; set; }
    public string Escape      { get; set; }
    public Font   StrongFont  { get; set; }
    public Color  StrongColor { get; set; }

    private SolidBrush textBrush, backBrush, strongBrush;

    protected override void OnPaint(PaintEventArgs e) { FmtLabel_Paint(this, e); }

    public void FmtLabel_Paint(object sender, PaintEventArgs e)
    {
        if (StrongColor == Color.Empty) StrongColor = SystemColors.ControlText;
        textBrush = new SolidBrush(ForeColor);
        backBrush = new SolidBrush(BackColor);
        strongBrush = new SolidBrush(StrongColor);

        e.Graphics.FillRectangle(backBrush, this.ClientRectangle);
        if (this.Text.Length <= 0) return;

        if (Splitter == null) Splitter = "*";
        int strongStart = Text.StartsWith(Splitter) ? 0 : 1;

        StringFormat SF = new StringFormat(StringFormat.GenericTypographic)
                    { FormatFlags = StringFormatFlags.MeasureTrailingSpaces };

        float x = 0f;  // tracks the write pointer

        string text_ = escapes(this.Text, true);
        string[] parts = text_.Split(new string[] { Splitter }, 
                                     StringSplitOptions.RemoveEmptyEntries);
        // plain and strong text will alternate
        for (int i = 0; i < parts.Length; i++)
        {
           string p = escapes(parts[i], false);
           if (i % 2 == strongStart)
           {
              e.Graphics.DrawString(p, StrongFont, textBrush, new Point((int)x, 0));
              x += e.Graphics.MeasureString(p, StrongFont, Point.Empty, SF).Width + 2;
           }
           else
           {
              e.Graphics.DrawString(p, this.Font, textBrush, new Point((int)x, 0));
              x += e.Graphics.MeasureString(p, this.Font, Point.Empty, SF).Width + 2;
           }
        }

        this.Width = (int)x;
        textBrush.Dispose();
        backBrush.Dispose();
        strongBrush.Dispose();
    }

    string escapes(string input, bool escape)
    {
        if (escape)  return input.Replace(Escape + Splitter, ((char)1).ToString());
        else         return input.Replace(((char)1).ToString(), Splitter);  // unescape
    }

  }
}
Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
0

WinForms don't allow to do it.

but

You'll need to put the text into a RichTextBox and have the name as a separate Run in the Paragraph as in this example from the MSDN:

// Create a Run of plain text and some bold text.
Run myRun1 = new Run();
myRun1.Text = "A RichTextBox with ";
Bold myBold = new Bold();
myBold.Inlines.Add("initial content ");
Run myRun2 = new Run();
myRun2.Text = "in it.";

// Create a paragraph and add the Run and Bold to it.
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun1);
myParagraph.Inlines.Add(myBold);
myParagraph.Inlines.Add(myRun2);

// Add the paragraph to the RichTextBox.
MyRTB.Blocks.Add(myParagraph);
Farshad
  • 1,465
  • 1
  • 9
  • 12