How do I set a textbox's text to bold at run time?

Diskdrive picture Diskdrive · Jun 22, 2010 · Viewed 147.9k times · Source

I'm using Windows forms and I have a textbox which I would occassionally like to make the text bold if it is a certain value.

How do I change the font characteristics at run time?

I see that there is a property called textbox1.Font.Bold but this is a Get only property.

Answer

Tim Lloyd picture Tim Lloyd · Jun 22, 2010

The bold property of the font itself is read only, but the actual font property of the text box is not. You can change the font of the textbox to bold as follows:

  textBox1.Font = new Font(textBox1.Font, FontStyle.Bold);

And then back again:

  textBox1.Font = new Font(textBox1.Font, FontStyle.Regular);