简体   繁体   中英

Design-time size of Label (Compact Framework)

I refer to the Visual Studio design view. When adding a label using the full .NET framework, System.Windows.Forms.Label controls have an AutoSize property set to True by default. If I drop a Label control onto a form, the default font is "Microsoft Sans Serif, 12pt, Regular" and the height of the control is 20. Change the font to (say) "Tahoma, 14pt, Bold" and the height automatically changes to 23. Fine.

But in the CF (Compact-Framework), Label controls do not have the AutoSize property. When I change the font as described above the height of the Label does not change (staying at 20) and some of the text is chopped off).

My question is: Short of opening a full .NET project and testing my font selection, is there a way to know what height to set my Label at design-time?

You can use the graphics object for to measure the height of the string, for example in Paint event or OnPaint method:

Graphics g = e.Graphics;
float lineHeight = g.MeasureString(this.Text, this.Font).Height;

it can be used in inherited label. for example you can override OnPaint method or somewhere else and do this, then it will run in design-time.

More resources:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM