116

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.

Diskdrive
  • 18,107
  • 27
  • 101
  • 167

5 Answers5

218

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);
Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
  • 1
    thanks! wow, that was much easier than i imagined. So I guess that means a font is like a string, once you create it, you can't change it. you can only declare a new instance of it. – Diskdrive Jun 21 '10 at 23:05
  • 2
    Yes it appears to behave like string in terms of not being able to change its state once created i.e. it is [immutable](http://en.wikipedia.org/wiki/Immutable_object). However, although there are MSDN articles that refer to Font being immutable, the actual reference for Font itself does not state this. – Tim Lloyd Jun 21 '10 at 23:09
  • for a linkbutton this worked for me: button.Font.Bold = true – deebs Jul 01 '15 at 18:53
  • 1
    Can the same thing be done for partial text? I mean I want to highlight just a part of the text. – Anil May 15 '18 at 10:40
  • @Anil - To answer this and for posterity, yes. Instead of applying the style change to the font in general, you'd apply it to the selection font as follows: textBox1.SelectionFont = new Font(textBox1.Font, FontStyle.Bold); This, in conjunction with an AppendText and a subsequent change back to FontStyle.Regular (or whatever the initial state of the style would be), will achieve what you want. – BJS3D Aug 15 '21 at 22:03
4

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);
        }
    }
}
Robert Hui
  • 744
  • 4
  • 8
2

Here is an example for toggling bold, underline, and italics.

   protected override bool ProcessCmdKey( ref Message msg, Keys keyData )
   {
      if ( ActiveControl is RichTextBox r )
      {
         if ( keyData == ( Keys.Control | Keys.B ) )
         {
            r.SelectionFont = new Font( r.SelectionFont, r.SelectionFont.Style ^ FontStyle.Bold ); // XOR will toggle
            return true;
         }
         if ( keyData == ( Keys.Control | Keys.U ) )
         {
            r.SelectionFont = new Font( r.SelectionFont, r.SelectionFont.Style ^ FontStyle.Underline ); // XOR will toggle
            return true;
         }
         if ( keyData == ( Keys.Control | Keys.I ) )
         {
            r.SelectionFont = new Font( r.SelectionFont, r.SelectionFont.Style ^ FontStyle.Italic ); // XOR will toggle
            return true;
         }
      }
      return base.ProcessCmdKey( ref msg, keyData );
   }
Derek
  • 7,615
  • 5
  • 33
  • 58
1

You could use Extension method to switch between Regular Style and Bold Style as below:

static class Helper
    {
        public static void SwtichToBoldRegular(this TextBox c)
        {
            if (c.Font.Style!= FontStyle.Bold)
                c.Font = new Font(c.Font, FontStyle.Bold);
            else
                c.Font = new Font(c.Font, FontStyle.Regular);
        }
    }

And usage:

textBox1.SwtichToBoldRegular();
Ali
  • 3,373
  • 5
  • 42
  • 54
0
 txtText.Font = new Font("Segoe UI", 8,FontStyle.Bold);
 //Font(Font Name,Font Size,Font.Style)
Joe Taras
  • 15,166
  • 7
  • 42
  • 55