Reputation:
My label is wrapping the text due to the length of the text. The height property returns the correct value only if there is a single line. How can I get the correct height?
Thanks.
Solution: I was creating the label dynamically and checking the height then. Later the panel on which the label was residing was added to a form, changing the panel's font and thus also changing the label's font and height.
Upvotes: 0
Views: 3216
Reputation:
Solution: I was creating the label dynamically and checking the height then. Later the panel on which the label was residing was added to a form, changing the panel's font and thus also changing the label's font and height.
Upvotes: 0
Reputation: 5955
Option 1: You can use Graphics.MeasureString or TextRenderer.MeasureText. The second one is probably easier for your purposes.
Option 2: If the label is not growing properly to fit the entire text, make sure that the AutoSize property is set to True.
Option 3: Use a TextBox instead of a Label. Set the ReadOnly property to true, and change the backcolor and border to match a Label. Then, set MultiLine = True. That may give you the same effect, but without whatever bug you are seeing.
Upvotes: 1
Reputation: 24232
The easiest way to get the preferred dimensions of a label is by using Label.GetPreferredSize(Size.Empty)
. If you wish to get the dimensions constrained by a size, use the same method with a non-empty size: Label.GetPreferredSize(constrainingSize)
Upvotes: 3