简体   繁体   中英

Adjust label height to height of string

So I'm making a C# Windows Forms application in which I have a label. This label's size is 100x100 by default*, but I want to automatically increase the label's height so that any string fits in it regardless of its "height". How would I do this? I haven't tried anything myself yet because I don't really have an idea of what to do. I'm just a beginner, after all!

Thanks in advance.

*100x100 is just an example, the real size is different (I'm not sure yet what it'll be)

Thanks everyone, I managed to get what I want by setting the label's MaximumSize property. (Answered by @LarsTech in comments)

AutoSize默认为true-因此,文本框的默认行为应调整为使用的任何字体大小/内容。

The label control in Windows Forms is a container which accepts your input strings. Therefore, if you do change its initial values it will get to its content size anyways. So, just change its Text Property. You could try it first.

here's an example code you can use. It uses the AutoSize property.

If you label is called Label1 you can change it like this: Label1.AutoSize = true; Label1.Text = "The text in this label is longer than the set size.";

And it will automatically change it.

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