简体   繁体   中英

Width of dynamic created Label in runtime on C#

I create Label in runtime: Label myLabel = new Label {Text = "somText"}; . After that myLabel.Width equals default value 104 . How I can know real width of myLabel ?

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

The real width is 104. Labels have a property called 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.

你试过myLabel.ActualWidth吗?

Surely the width is the width? Remeber to disbale Autosize if you want to fix the width at a certain value.

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