Part of the Label in Bold?

Asked

Viewed 6,055 times

3

How to place a part of the string of a Label in the Windows Form in bold via code in c#. Someone knows something via code for that?

  • I don’t know, but it’s worth trying <b></b> and [b][/b]

  • @fotanus did not run any of those 2. Obg by attempt. :)

  • @Silvioandorinha In the swing of Java it is possible to use html tags. <html>This would be a text <b>partially</b> in bold in a Jlabel.</html>

  • I find this functionality essential in c# to have more options for formatting the text. = what a pity. Obg

  • @Silvioandorinha the problem is not the language, but the graphical library in use.

6 answers

4

Cannot do this with a single type control Label, since all he does is render a single string no formatting information. You need two or more controls to visually assemble your text if you are going to use Labels.

I do not recommend this, as formatting can become increasingly complex if you need to keep the text concise when the form is resized, or if the user has configured Windows to use a different font size than standard.

To get what you want without appealing to various controls Label, you can:

  • Create your own control that takes care of text rendering;
  • Use an image GIF or PNG with transparent background instead of a Label (maybe it’s the easiest alternative);
  • Use another form of interface, such as WPF.
  • 1

    I’ll try that! obg Renan ;)

3


You won’t get.

You can use Richtext control that recognizes partial formatting in text.

  • Does Label not offer this type of support? style the code '/r' to break the line. Obg.

  • Really at first there would be no way to change a part of the text and in Richtext I already knew I had how. But it would not serve for what I intend to do. Obg @Raul-Almeida

1

Use:

SeuLabel.Font = New Font(SeuLabel.Font, FontStyle.Bold)
  • I would like to put a bold part, not the whole text. Obg by trying.

1

Alternative TO: Works well.

John, Joseph and Mary

lblTitulo.Text = 'João, <b>José</b> e Maria';

 


Alternative B:

It’s not ideal, but you can do it using several Abels and this would give you the control to change by code Behind the exact value of the region in bold:

John, Joseph and Mary

// Se o negrito estiver no início, deverá existir duas labels
// uma do lado da outra, a primeira seria lblTituloNegrito e a outra lblTituloNormal
lblTituloNegrito.Font = New Font(SeuLabel.Font, FontStyle.Bold);
lblTituloNegrito.Text = 'João';
lblTituloNormal.Text = ', José e Maria';

John, Joseph and Maria

// Se o negrito estiver no fim, deverá existir duas labels
// uma do lado da outra, a primeira seria lblTituloNormal e a outra lblTituloNegrito 
lblTituloNormal.Text = 'João, José e ';
lblTituloNegrito.Font = New Font(SeuLabel.Font, FontStyle.Bold);
lblTituloNegrito.Text = 'Maria';

John, Joseph and Mary.

// Se o negrito estiver no meio, deverá existir três labels
// uma do lado da outra, a primeira seria lblTituloNormalInicio,
// a segunda lblTituloNegrito e a terceira lblTituloNormalFim
lblTituloNormalInicio.Text = 'João, ';
lblTituloNegrito.Font = New Font(SeuLabel.Font, FontStyle.Bold);
lblTituloNegrito.Text = 'José';
lblTituloNormalFim.Text = 'e Maria';

 


There are other alternatives. A alternative to always attended to me. Already had a specific case that I thought better to use the alternative B but it was literally a one-off problem.

  • 1

    It would do to the well half hehe mouth. But vlw by suggestion ! ;)

0

There is the open source project HTML Renderer that allows this you are asking for.

He has the control HtmlLabel which enables the use of tags (Ex: texto em <b>negrito</b>) as already suggested in comments on your question.

0

Place a DIV by adding the 'runat' and 'id' attributes'

<div id="textoDIV" runat="server"></div>

In code Behind call the div and add your text by the property 'innerhtml'

textoDIV.InnerHtml = "<a href='/'>Stackoverflow em <strong>português!</strong></a>";

Ready!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.