简体   繁体   English

C#中运行时动态创建标签的宽度

[英]Width of dynamic created Label in runtime on C#

I create Label in runtime: Label myLabel = new Label {Text = "somText"}; 我在运行时创建Label: Label myLabel = new Label {Text = "somText"}; . After that myLabel.Width equals default value 104 . 之后myLabel.Width等于默认值104 How I can know real width of myLabel ? 我怎么知道myLabel真实宽度?

SizeF size = myLabel.CreateGraphics().MeasureString(myLabel.Text, myLabel.Font);

The real width is 104. Labels have a property called AutoSize. 实际宽度为104.标签有一个名为AutoSize的属性。 The width will expand or contract depending on the text you set. 宽度将根据您设置的文本扩展或收缩。 You can set the AutoSize property to false and set your own width though. 您可以将AutoSize属性设置为false并设置自己的宽度。

你试过myLabel.ActualWidth吗?

Surely the width is the width? 当然宽度是宽度? Remeber to disbale Autosize if you want to fix the width at a certain value. 如果要将宽度固定在某个值,请记住取消Autosize大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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