无法在 C# 中更改标签的字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18081648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't change font size of label in C#
提问by imulsion
I am using Visual Studio 2010 to build a very simple form. It all works fine except I need to dynamically draw a label onto the form. I can draw the label, but when I try and change the newlabel.Font.Size
attribute, I get this error:
我正在使用 Visual Studio 2010 构建一个非常简单的表单。一切正常,除了我需要在表单上动态绘制标签。我可以绘制标签,但是当我尝试更改newlabel.Font.Size
属性时,出现此错误:
Property or indexer 'System.Drawing.Font.Size' cannot be assigned to -- it is read only
What does this mean and how can I fix it? This is my first ever C# program, so please cut me some slack if I'm doing something really stupid.
这是什么意思,我该如何解决?这是我的第一个 C# 程序,所以如果我在做一些非常愚蠢的事情,请让我放松一下。
Here is my code for drawing the label:
这是我绘制标签的代码:
Label newlabel = new Label();
newlabel.Text = "BOOM";
newlabel.Font.Size = 72;//This causes the error
newlabel.ForeColor = Color.White;
newlabel.Location = new Point(250,250);
newlabel.AutoSize = false;
this.Controls.Add(newlabel);
采纳答案by Magn3s1um
You have to create a new font using: newlabel.Font = new Font(fontFamily, size);
您必须使用以下方法创建新字体: newlabel.Font = new Font(fontFamily, size);
回答by Ajay
try this
尝试这个
newlabel.Font = new Font(newlabel.Font.FontFamily, Fontsize);
回答by Joseph Devlin
Label newlabel = new Label();
newlabel.Font = new System.Drawing.Font(l.Font.FontFamily.Name, 12);
回答by onur
var label1 = new Label();
label1.Font = new Font(new FontFamily(System.Drawing.Text.GenericFontFamilies.Serif), 10);
回答by Nathangrad
Firstly, to explain the following error:
首先解释一下下面的错误:
Property or indexer 'System.Drawing.Font.Size' cannot be assigned to -- it is read only
无法分配属性或索引器“System.Drawing.Font.Size”——它是只读的
This is a compile-time error regarding the accessibility of a variable. In your example, System.Drawing.Font.Size
cannot be modified because the property within the Font
class is declared as public float Size { get; }
. You're able to view this by right-clicking Size
and clicking on "Go to definition".
The property declaration shown above describes a float value called Size which has a public 'getter' method - meaning you're able to retreive a value from that property.
It has no 'setter' property which makes modification impossible.
Since the property is unable to be changed, you will need to create a new Font
by changing the Font
property to something like new Font("Times New Roman", 12.0f);
. Take a look at the following MSDN documentationwhich provides all the different constructors for the Font
class.
A working example is shown below for your convenience:
这是关于变量可访问性的编译时错误。在您的示例中,System.Drawing.Font.Size
无法修改,因为Font
类中的属性声明为public float Size { get; }
. 您可以通过右键单击Size
并单击“转到定义”来查看此内容。
上面显示的属性声明描述了一个名为 Size 的浮点值,它有一个公共的“getter”方法——这意味着你可以从该属性中检索一个值。
它没有“setter”属性,因此无法进行修改。
由于该属性无法更改,您需要Font
通过将Font
属性更改为类似new Font("Times New Roman", 12.0f);
. 看看下面的MSDN文档它为Font
类提供了所有不同的构造函数。
为方便起见,下面显示了一个工作示例:
Label newlabel = new Label {
Text = "BOOM",
Font = new Font("Times New Roman", 12.0f),
ForeColor = Color.White,
Location = new Point(250, 250),
AutoSize = false
};
this.Controls.Add(newlabel);