Display font names in combobox and change the style of the text accordingly

Ddd
Posted by Ddd under Windows Forms category on | Points: 40 | Views : 5275
Open a windows application. Take a combobox and a richtextbox.

Type this code in Form1.cs file


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
foreach (FontFamily f in FontFamily.Families)
{
comboBox1.Items.Add(f.Name);

}
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = comboBox1.SelectedItem.ToString();
Font f = new Font(s, 30f, FontStyle.Bold);
richTextBox1.Font = f;

//30f is the font size. f has been suffixed as size is of float datatype. (kindly see the tooltip) as you type.
}
}
}

Comments or Responses

Login to post response