Apply Font Style and Size to the Selected Text in C#

Introduction

We can change the font style and find size of the selected text in Rich TextBox, which is uniquely applied to the preferred text.

If you want to apply both, say bold and italic to the selected text, you can apply bold and then italic to the selected text, Also check for font size to the selected text.

  1. Form design i given below.

    design

  2. Code to change the font size and font style is given below.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Drawing;  
    6. using System.Linq;  
    7. using System.Text;  
    8. using System.Threading.Tasks;  
    9. using System.Windows.Forms;  
    10.   
    11. namespace WindowsFormsApplication31  
    12. {  
    13.     public partial class Form1 : Form  
    14.     {  
    15.         public Form1()  
    16.         {  
    17.             InitializeComponent();  
    18.         }  
    19.   
    20.         private void Form1_Load(object sender, EventArgs e)  
    21.         {  
    22.             btnUnderline.Text = "Underline";  
    23.             btnUnderline.Font = new Font("Arial", 10, FontStyle.Underline,GraphicsUnit.Point);  
    24.             AddFontSize();  
    25.   
    26.         }  
    27.   
    28.         private void AddFontSize()  
    29.         {  
    30.            // throw new NotImplementedException();  
    31.             comboBoxSize.Items.Add("8");  
    32.             comboBoxSize.Items.Add("9");  
    33.             comboBoxSize.Items.Add("10");  
    34.             comboBoxSize.Items.Add("11");  
    35.             comboBoxSize.Items.Add("12");  
    36.               
    37.         }  
    38.   
    39.         private void btnBold_Click(object sender, EventArgs e)  
    40.         {  
    41.             rtfText.SelectionFont = new Font(rtfText.SelectionFont, rtfText.SelectionFont.Style | FontStyle.Bold);  
    42.         }  
    43.   
    44.         private void btnItalic_Click(object sender, EventArgs e)  
    45.         {  
    46.             rtfText.SelectionFont = new Font(rtfText.SelectionFont, rtfText.SelectionFont.Style | FontStyle.Italic);  
    47.         }  
    48.   
    49.         private void btnUnderline_Click(object sender, EventArgs e)  
    50.         {  
    51.             rtfText.SelectionFont = new Font(rtfText.SelectionFont, rtfText.SelectionFont.Style | FontStyle.Underline);  
    52.         }  
    53.   
    54.         private void btnStrike_Click(object sender, EventArgs e)  
    55.         {  
    56.             rtfText.SelectionFont = new Font(rtfText.SelectionFont, rtfText.SelectionFont.Style | FontStyle.Strikeout);  
    57.         }  
    58.   
    59.         private void comboBoxSize_SelectedIndexChanged(object sender, EventArgs e)  
    60.         {  
    61.             FontChange();  
    62.         }  
    63.   
    64.         private void FontChange()  
    65.         {  
    66.            // throw new NotImplementedException();  
    67.             float fontsize = 10;  
    68.             string fontname = rtfText.SelectionFont.Name;  
    69.   
    70.             if (comboBoxSize.Text != "") fontsize = float.Parse(comboBoxSize.Text);  
    71.            
    72.             if (fontsize == 0) fontsize = 10;  
    73.             if (rtfText.SelectionLength > 0)  
    74.             {  
    75.                 rtfText.SelectionFont = new Font(fontname, fontsize);  
    76.             }  
    77.         }  
    78.   
    79.     }  
    80. }  

Note:

  • We can apply the “Underline” font style to the button text by using the code given below in form load.
    1. button.Text = "Underline";  
    2. button.Font=newFont("Arial",10,FontStyle.Underline,GraphicsUnit.Point);  
  • For Combobox, you have to write in “comboBoxSize_SelectedIndexChanged” event.