Reputation: 7993
In my project I need to create some label at runtime. These have a different height that depends from a field of a database. I need to write some text in the label, but I need, if the label have a small height, to resize the font depending by the label height.
I've tried with this:
Label lbl = new Label();
lbl.AutoSize = false;
lbl.Font = new System.Drawing.Font(lbl.Font.FontFamily, Convert.ToSingle(lbl.Height / 2));
lbl.TextAlign = ContentAlignment.MiddleCenter;
Upvotes: 0
Views: 3519
Reputation: 109547
If you set Label.AutoSize to false, then you must programatically set the height of the label. You can't start using Label.Height to determine the size of the font, since Label.Height will be its default value (something like 23). That will have no correspondence to the height read from the database.
What exactly IS the field in the database that you are using to specify the label height? What units is it in? You are not using any database field in the code that you posted...
Upvotes: 0
Reputation: 4532
You can use the FontHeight property to change the height of a font. This could do the job.
MSDN:
Gets or sets the height of the font of the control. ... The FontHeight property should not be set to any value other than the control's Font.Height value, or -1.Setting FontHeight to -1 has the effect of clearing the cached height value, and the value is recalculated the next time the property is referenced.
Upvotes: 1