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

前端 未结 5 1415
野的像风
野的像风 2020-12-08 12:44

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 characteristi

5条回答
  •  隐瞒了意图╮
    2020-12-08 13:23

    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 );
       }
    

提交回复
热议问题