1

i have a textbox and listbox listbox show all of the font in the system. Textbox use for enter a string. i don't know when user click any item in list box then font of the textbox will be changed. and mycode only use to show all of font in system. i'm newbie

    private void Form1_Load(object sender, EventArgs e)
    {
        InstalledFontCollection fonts = new InstalledFontCollection();
        try
        {
            foreach (FontFamily font in fonts.Families)
            {
                FontListBox.Items.Add(font.Name);
            }
        }
        catch (Exception)
        {

            MessageBox.Show(e.ToString());
        }
    }
2
  • Please clarify your problem and question, so we don't waste our time to guess. Welcome to Stackoverflow :) Commented Mar 22, 2018 at 14:09
  • 1
    thank you. i have a form include a listbox and text box, i wanna enter a string in textbox and in listbox i wanna show all of system font. when i select a font form listbox . the font of string in textbox will change. Commented Mar 22, 2018 at 14:17

1 Answer 1

1

You should use SelectedIndexChanged event to catch when another font selected by user. Later You can find the font by its name and apply to txt.Font.

    InstalledFontCollection fonts;
    private void Form1_Load(object sender, EventArgs e)
    {
        InstalledFontCollection fonts = new InstalledFontCollection();
        try
        {
            foreach (FontFamily font in fonts.Families)
            {
                listBox1.Items.Add(font.Name);
            }
        }
        catch (Exception)
        {

            MessageBox.Show(e.ToString());
        }
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        textBox1.Font = new Font(listBox1.SelectedItem.ToString(), textBox1.Font.Size);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

My bad, sorry. I updated the code, please try again.
@Akiko Make sure you have subscribed to the SelectedIndexChanged event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.