Dave Ryan Dave Ryan - 1 year ago 111
C# Question

c# Button font.height can't be set to Double Values. Can't convert from double to float

I have a button named button1. Using toolbox i can set the button font height to a double value, using properties then font,then size and i can set to 30.251. but when i set it programmatically, i cant write 30.251.

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
public partial class Form1 : Form
public Form1()
double value=90.753/3;//the numerator & denominator came from certain computations

Button button1 = new Button();
button1.Font = new Font("Arial", value, FontStyle.Bold);

What is the problem?I need an exact value of font height up to 3decimal places. It show an error "Can't convert from double to float".

Answer Source

Font size is float type not double Try:

button1.Font = new Font("Arial", 30.251f, FontStyle.Bold);

Or if the value is in a double property use:

button1.Font = new Font("Arial", (float)fontSize, FontStyle.Bold);

Or if you want to get the number earlyer

float value=(float)(90.753/3);//the numerator & denominator came from certain computations

Or more clean

float value=90.753f/3;//the numerator & denominator came from certain computations
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download