Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 760
Diskdrive Avatar asked Jun 21 '10 22:06

Diskdrive


People also ask

How do I make certain text bold?

Type the keyboard shortcut: CTRL+B.

How do you make a text box bold?

With the cursor on top of the text box, right click, then select Font and then Font Style "Bold".

How do I make my font bolder in Windows?

How do I make text bold in Windows 10? Microsoft tells you to go to your display settings and scroll down to the botton, click "Advanced Display Settings" and then to check the checkbox next to "Bold." However, this is not where Advanced Display Settings are located.

How do I make text bold in RTF?

In order to make the text bold you just need to surround the text with \b and use the Rtf member.


2 Answers

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); 
like image 110
Tim Lloyd Avatar answered Sep 29 '22 16:09

Tim Lloyd


Depending on your application, you'll probably want to use that Font assignment either on text change or focus/unfocus of the textbox in question.

Here's a quick sample of what it could look like (empty form, with just a textbox. Font turns bold when the text reads 'bold', case-insensitive):

public partial class Form1 : Form {     public Form1()     {         InitializeComponent();         RegisterEvents();     }      private void RegisterEvents()     {         _tboTest.TextChanged += new EventHandler(TboTest_TextChanged);     }      private void TboTest_TextChanged(object sender, EventArgs e)     {         // Change the text to bold on specified condition         if (_tboTest.Text.Equals("Bold", StringComparison.OrdinalIgnoreCase))         {             _tboTest.Font = new Font(_tboTest.Font, FontStyle.Bold);         }         else         {             _tboTest.Font = new Font(_tboTest.Font, FontStyle.Regular);         }     } } 
like image 31
Robert Hui Avatar answered Sep 29 '22 16:09

Robert Hui