C#: Cannot update control height while resizing
I have implemented a UserControl
with a text Label
. I want to adjust the control height when I resize the control width, to make the whole text visible all the time, like in the picture:
I have customized the WinForms Label
to calculate the TextBounds
of the Label (the needed rectangle fo开发者_JAVA百科r the text) and I raise an event called TextBoundsChanged
that indicates that the text rectangle size has changed (Note that this event is raised always while the label is being resized).
This works: I receive the event, then, I update my control height, but the height of the control really doesn't change!!
I suspect that the TextBoundsChanged
event is raised when the UserControl
is being resized. The UserControl
is being resized also, so I'm afraid that WinForms ignore the control height update while resizing.
Am I right? Someone know how to workaround this issue?
Thanks in advance.
EDIT: The user control not only show text. This is only a simplified sample. So, my user control needs to vertically grow when I reduce the width, in order to show the whole text in the label. the label is not a custom winforms label, because it renders HTML content. Also, I'm using .NET1, so the word wrap property is not available for me
Simply set the Dock
or Anchor
property of the Label within your UserControl and everything will work like a charme (you can also inspect the correct behaviour at design-time, by just resizing your UserControl). ;-)
Does it have to be a UserControl
? The behavior you want would be quite easy if you docked the label to the top of the form, turned autosize off and enabled word wrap.
"WinForms ignore the control height update while resizing"
You mean UserControl ? I Highly doubt it
Make sure that your events go through all the right pipes/controls and perform invalidation when needed for the display (in the userContol or/and the embedding form). That normally shoudl do. If not, Use Docking as Oliver suggested.
精彩评论