【问题标题】:c# Button font.height can't be set to Double Values. Can't convert from double to floatc# Button font.height 不能设置为双精度值。无法从双精度转换为浮点
【发布时间】:2016-11-08 06:10:09
【问题描述】:

我有一个名为 button1 的按钮。使用工具箱,我可以将按钮字体高度设置为双精度值,使用属性然后字体,然后大小,我可以设置为 30.251。但是当我以编程方式设置它时,我不能写 30.251。

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

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

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

有什么问题?我需要一个精确的字体高度值,最多 3 个小数位。它显示错误“无法从双精度转换为浮点数”。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    字体大小是浮点型而不是双倍 试试:

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

    或者如果该值在双重属性中使用:

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

    或者如果你想早点得到号码

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

    或者更干净

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

    【讨论】:

    • 我忘记了什么。 30.251 来自某个计算。值=一些计算/一些计算。查看我的编辑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    相关资源
    最近更新 更多