3
Reply

Display font dialog size from FontDialog into combo box

Stephen Walsh

Stephen Walsh

Apr 16 2013 5:39 AM
1.7k
Sorry if this is a little confusing. I am quite new to c#. I am creating a text editor & I have hit a problem using a font dialog & selecting size. Basically I want the font family & size selected in the font dialog to be displayed in 2 combo boxes. I have managed to do this with the family but not the size.

Here is my code:

        private void tsbtnChooseFont_Click(object sender, EventArgs e)
        {
            using (FontDialog fdlg = new FontDialog())
            {
                if (txtMain.SelectionFont != null)
                    if (fdlg.ShowDialog() == DialogResult.OK)
                    {
                        txtMain.SelectionFont = fdlg.Font;
                        tscmbFont.Text = ""; // clear text before adding new
                        tscmbFont.SelectedText = fdlg.Font.FontFamily.Name.ToString();
                        tscmbFontSize.Text = "";
                        tscmbFontSize.SelectedText = fdlg.Font.Size.ToString();
                    }
            }
        }

The names are pretty self explanatory but I will explain anyway.
txtMain is the richtextbox to type in
tscmbFont is the combo box which displays the font family
tscmbFontSize is the combo box which displays the font size.

I'm guessing the problem lies with the line "tscmbFontSize.SelectedText = fdlg.Font.Size.ToString();" for example when size 14 is selected the box displays 25 & when size 16 is selected the box displays 75.

Any help would be appreciated. Thanks in advance.
 

Answers (3)