pfinferno
pfinferno

Reputation: 1945

Retrieving each character of a selected FontFamily

I'm trying to create a control that allows the user to select a font family, and then pick a character from that font family. Below is an example of what I'm trying to achieve.

enter image description here

Populating a combobox with the list of font families was easy enough:

FontFamilyCB.DisplayMember = "Name";
foreach (System.Drawing.FontFamily font in System.Drawing.FontFamily.Families)
{
    FontFamilyCB.Items.Add(font);
}

But how do I loop through and retrieve each specific character in the given font family?

I'll be displaying each character on a 'DataGridViewButtonCell'.

Upvotes: 1

Views: 487

Answers (2)

Rufus L
Rufus L

Reputation: 37020

Here's an example of how to display characters by casting a int to a char. If we do this in a loop (and for enough iterations), we will end up displaying all the characters. Unfortunately, I think there's over 100,000 Unicode characters, which is probably more than you want to display (and not all of them will display something for every font).

I'll leave it up to you to figure out which ones you really want to display, but here's a sample that prints out the first 3,750 (75 x 50) of them in a grid of buttons. *Note that the load time gets slower and slower with the more controls you add.

Just create a new WinForms project and put this code into the Form_Load event. You can adjust the grid size (and therefore the load time) using the variables defined:

private void Form1_Load(object sender, EventArgs e)
{
    int gridWidth = 75;
    int gridHeight = 50;
    int controlSize = 20;

    int row = 0;

    for (int i = 1; i < gridWidth * gridHeight; i++)
    {
        var value = ((char) i).ToString();

        Controls.Add(new Button
        {
            Left = i % gridWidth * controlSize,
            Top = row * controlSize,
            Width = controlSize,
            Height = controlSize,
            Text = value
        });

        if (i % gridWidth == 0) row++;
    }
}

Output

enter image description here

Upvotes: 2

Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

DataGridViewButtonCell has a property of Style.Font.FontFamily .
here is a full example using DataGridViewButtonCell:

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

    private void Form1_Load(object sender, EventArgs e)
    {
        LoadDgv();

        // detect each button font family
        for (int i = 0; i < dataGridView1.RowCount-1; i++)
        {
            Debug.WriteLine(dataGridView1.Rows[i].Cells["button"].Style.Font.FontFamily.ToString());
        }
    }

    private void LoadDgv()
    {
        dataGridView1.Columns.Add("btns", "BTNS");
        FontFamilyCB.DisplayMember = "Name";
        DataGridViewButtonColumn button = new DataGridViewButtonColumn();
        {
            button.Name = "button";
            button.HeaderText = "Button";
            button.Text = "Button";
            dataGridView1.Columns.Add(button);
        }
        foreach (System.Drawing.FontFamily font in System.Drawing.FontFamily.Families)
        {
            FontFamilyCB.Items.Add(font);

            DataGridViewButtonCell b = new DataGridViewButtonCell();

            int rowIndex = dataGridView1.Rows.Add(b);
            dataGridView1.Rows[rowIndex].Cells["button"].Style.Font = new Font(font, 11, FontStyle.Regular);
            // each cell will have his own font-family
            dataGridView1.Rows[rowIndex].Cells["button"].Value = "A";
        }
    }
}

Upvotes: 1

Related Questions