ib11
ib11

Reputation: 2558

C# combobox with fonts using Datasource Visual Studio 2013

I have a form with a ComboBox on it. I would like to fill this with the available fonts on the system and make the user to select one of these options.

I looked for different approaches to achieving this and I used this question and the answer to load the ComboBox with all the fonts: Fill ComboBox with List of available Fonts

This is my code currently that works:

form.comboBox2.Items.Clear();

System.Drawing.Text.FontCollection fontcoll = new System.Drawing.Text.InstalledFontCollection();

foreach (FontFamily font in fontcoll.Families)
{
    form.comboBox2.Items.Add(font.Name);
}

But now I am trying to use instead the DataSource property and I imported the System.Drawing.Text.InstalledFontCollection into my project as datasource.

Here is the code of the designer:

// 
// comboBox2
// 
this.comboBox2.DataSource = this.installedFontCollectionBindingSource;
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Location = new System.Drawing.Point(16, 44);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(144, 21);
this.comboBox2.TabIndex = 9;
this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);

Then in my initialization of the form, I have this to set selected the font name to Times New Roman as default:

form.comboBox2.Text = "Times New Roman"

I thought this would be enough to fill the ComboBox and select Times New Roman but apparently is not enough. It displays Times New Roman alright, but the box is empty.

What I would like to get help with:

1) How to make the datasource to populate the ComboBox?

2) Is there a simple way to force the user to select one of the entries from the box and not type in some other value that is not in the list (similarly to the "MatchRequired" property in VBA userforms)?

Thanks in advance.

Upvotes: 1

Views: 743

Answers (2)

Abdellah OUMGHAR
Abdellah OUMGHAR

Reputation: 3745

You can use :

private void Form1_Load(object sender, EventArgs e)
{
    FontFamily[] fontArray = FontFamily.Families;
    foreach (FontFamily font in fontArray)
    {
        comboBox1.Items.Add(font.Name);
    }
    comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

}

With a property DropDownStyle, users are limited to choices in the list.

For example if you want to assign font with size 14 to the Label :

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    label1.Font = new Font(comboBox1.Text , 14);
}

Upvotes: 1

Reza Aghaei
Reza Aghaei

Reputation: 125187

You should first get a list of all installed font families and then set the list as DataSource of ComboBox. Also you can set DropDownStyle of combo box to DropDownList.

private void Form1_Load(object sender, EventArgs e)
{
    this.comboBox1.DataSource = new InstalledFontCollection().Families;
    this.comboBox1.DisplayMember = "Name";
    this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
    this.comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}

You can get selected font family from SelectedValueof ComboBox. For example:

void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (this.comboBox1.SelectedValue != null)
        this.Font = new Font((FontFamily)this.comboBox1.SelectedValue, this.Font.Size);
}

Upvotes: 1

Related Questions