How To Apply Font Size And Style To The Selected Text In C# Using Microsoft Visual Studio

Prerequisites
  • Visual Studio 2015. 
Step 1

Go to Visual Studio 2015 and select "File-----> New----->Project".
 
 
Step 2

Choose 'Windows' and select 'Windows Forms Application', give any name in Namebox and select OK button.
 
 
Step 3 

Subsequently, design a form, using button, label, and text box to change the Bold, Italic, Underline, and Strike out to the selected text, if you want to apply both Bold and Italic to the selected text, and also font size to the selected text.
 
 
Step 4

Now, code to change the font style, size, and underline the selected text in rich TextBox, which is uniquely applied to the preferred text. 
  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. namespace WindowsFormsApplication31 {  
  11.     public partial class Form1: Form {  
  12.         public Form1() {  
  13.             InitializeComponent();  
  14.         }  
  15.         private void Form1_Load(object sender, EventArgs e) {  
  16.             btnUnderline.Text = "Underline";  
  17.             btnUnderline.Font = new Font("Arial", 10, FontStyle.Underline, GraphicsUnit.Point);  
  18.             AddFontSize();  
  19.         }  
  20.         private void AddFontSize() {  
  21.             // throw new Not Implemented Exception();    
  22.             comboBoxSize.Items.Add("8");  
  23.             comboBoxSize.Items.Add("9");  
  24.             comboBoxSize.Items.Add("10");  
  25.             comboBoxSize.Items.Add("11");  
  26.             comboBoxSize.Items.Add("12");  
  27.         }  
  28.         private void btnBold_Click(object sender, EventArgs e) {  
  29.             rtfText.SelectionFont = new Font(rtfText.SelectionFont, rtfText.SelectionFont.Style | FontStyle.Bold);  
  30.         }  
  31.         private void btnItalic_Click(object sender, EventArgs e) {  
  32.             rtfText.SelectionFont = new Font(rtfText.SelectionFont, rtfText.SelectionFont.Style | FontStyle.Italic);  
  33.         }  
  34.         private void btnUnderline_Click(object sender, EventArgs e) {  
  35.             rtfText.SelectionFont = new Font(rtfText.SelectionFont, rtfText.SelectionFont.Style | FontStyle.Underline);  
  36.         }  
  37.         private void btnStrike_Click(object sender, EventArgs e) {  
  38.             rtfText.SelectionFont = new Font(rtfText.SelectionFont, rtfText.SelectionFont.Style | FontStyle.Strikeout);  
  39.         }  
  40.         private void comboBoxSize_SelectedIndexChanged(object sender, EventArgs e) {  
  41.             FontChange();  
  42.         }  
  43.         private void FontChange() {  
  44.             // throw new Not Implemented Exception();    
  45.             float fontsize = 10;  
  46.             string fontname = rtfText.SelectionFont.Name;  
  47.             if (comboBoxSize.Text != "") fontsize = float.Parse(comboBoxSize.Text);  
  48.             if (fontsize == 0) fontsize = 10;  
  49.             if (rtfText.SelectionLength > 0) {  
  50.                 rtfText.SelectionFont = new Font(fontname, fontsize);  
  51.             }  
  52.         }  
  53.     }  
  54. }   
For Combobox, you have to write in "comboBoxSize_SelectedindexChanged"event.
 
Conclusion

In this blog, we learned about font style and size in C#. Feel free to comment with some suggestions. Thank you.