How to bring partial text Bold in a TextBox?

2019-08-21 10:28发布

问题:

I have one textbox which has the following text.

TextBox1.Text = "The above materials will be delivered at Site. One copy of the Delivery Challan / Invoice to be send to Head Office."

In that, I want to make the following text "One copy of the Delivery Challan / Invoice to be send to Head Office" as in Capital Letters and also in Bold.

I tried the following coding:

TextBox1.Font.Bold = true;

But it made all the text as bold. How do I make it?

回答1:

Since you're using a web application, you can't use the RichTextBox component.

Instead, you can use a LiteralControl inside a span or a div.

i.e:

.aspx
<div runat="server" id="div1"></div>

.cs
div1.Controls.Add(new LiteralControl("this is normal text, "));
div1.Controls.Add(new LiteralControl("<b>And the rest of the sentence is bold</b>"));


回答2:

Since you are just distributing data and not allowing the user to edit the value I would suggest using a combo(or div span your choice) using Styles. It requires absolutely no server side code for example

<div id="normaltext" name="normaltext" class="textbox">
    <div id="boldtext" name="boldtext" class="textbox" style="font-weight:bold"></div>

</div>

You can then access either portion using javascript to getElementsById.

Alternatively if it isn't an HTML based object I would argue to use two labels instead of a textbox. One could be bold and the other normal.



回答3:

Put two textbox instead of one. In windows forms it's the only way. in Wpf, the answer is here.