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.
Loading
VulpesPosted Apr 16, 2013, 9:42 AM
tscmbFontSize.SelectedText = Math.Ceiling(fdlg.Font.Size).ToString();
and to round to the nearer integer I'd use this:
tscmbFontSize.SelectedText = Math.Round(fdlg.Font.Size).ToString();
Stephen WalshPosted Apr 16, 2013, 8:54 AM
Again this is probably a simple fix but how do I remove the decimal point & round the number up?
I have tried: tscmbFontSize.SelectedText = Convert.ToSingle(fdlg.Font.Size.ToString());
but get the error "Cannot implicitly convert type 'float' to 'string' "
Again thanks in advance
VulpesPosted Apr 16, 2013, 6:38 AM
The only theory I have is that the 25 and 75 might be the part of the size following the decimal point.
For example, if I choose Microsoft Sans Serif size 14 in the FontDialog, the ComboBox shows 14.25 and, if I choose size 16, it shows 15.75.
Do you have any other ComboBox events which could adjust the selected text in some way?