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?
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?
You will not be able to.
You can use the RichText control that recognizes partial text formatting.
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:
Label
or GIF
with transparent background instead of PNG
(perhaps the easiest alternative); Label
. Use:
SeuLabel.Font = New Font(SeuLabel.Font, FontStyle.Bold)
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.
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!
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.