Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change font size of label in C#

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:

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.

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);
like image 579
imulsion Avatar asked Aug 06 '13 13:08

imulsion


People also ask

How do I change font-size in labels?

To change the font size in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property font-size. HTML5 do not support the <font> tag, so the CSS style is used to add font size.

Which property is required to change the size of label?

To make a label control resize dynamically to fit its contents. Set its AutoSize property to true .


2 Answers

You have to create a new font using: newlabel.Font = new Font(fontFamily, size);

like image 150
Magn3s1um Avatar answered Sep 28 '22 05:09

Magn3s1um


try this

newlabel.Font = new Font(newlabel.Font.FontFamily, Fontsize);
like image 34
Ajay Avatar answered Sep 28 '22 04:09

Ajay