Part of Label in Bold?

3

How to put a part of string of a Label into Windows Form in bold via code in c# . Does anyone know anything via code for this?

    
asked by anonymous 12.02.2014 / 20:22

6 answers

3

You will not be able to.

You can use the RichText control that recognizes partial text formatting.

    
12.02.2014 / 20:25
4

You can not do this with a single control of type Label , since all it does is render a single string with no formatting information. You need two or more controls to assemble your text visually, if you 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 you have configured Windows to use a font size other than the default.

To get what you want without appealing to a number of%% controls, you can:

  • Create your own control that takes care of text rendering;
  • Use an image Label or GIF with transparent background instead of PNG (perhaps the easiest alternative);
  • Use another form of interface, such as Label .
12.02.2014 / 22:34
1

Use:

SeuLabel.Font = New Font(SeuLabel.Font, FontStyle.Bold)
    
12.02.2014 / 20:24
1

Alternative A: Works well.

John, Joseph and Mary

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

Alternative B:

It is not ideal, but you can do it using multiple labels and this would give you the control of changing the value exactly from the bold region in the code behind:

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 Mary

// 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. The alternative A has always answered me. I've already had a specific case that I thought it would be best to use the alternative B but it was literally a one-time problem.

    
13.02.2014 / 13:43
0

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

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

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

textoDIV.InnerHtml = "<a href='http://pt.stackoverflow.com/'>Stackoverflow em <strong>português!</strong></a>";

Ready!

    
19.03.2014 / 16:13
0

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

It has the control HtmlLabel that allows the use of tags (Ex: texto em <b>negrito</b> ) as already suggested in the comments in your question.

    
13.02.2014 / 15:11