C#:调整大小时无法更新控件高度
我已经实现了带有文本Label
的UserControl
。我想在调整控件宽度时调整控件高度,以使整个文本始终可见,如图所示:
我已经自定义了 WinForms Label
来计算 TextBounds
标签(文本所需的矩形),并引发一个名为 TextBoundsChanged
的事件,该事件指示文本矩形大小已更改(请注意,在调整标签大小时始终会引发此事件)。
这是可行的:我收到事件,然后,我更新我的控件高度,但控件的高度确实没有改变!
我怀疑调整 UserControl
大小时会引发 TextBoundsChanged
事件。 UserControl
也正在调整大小,因此我担心 WinForms 在调整大小时会忽略控件高度更新。
我说得对吗?有人知道如何解决这个问题吗?
提前致谢。
编辑:用户控件不仅显示文本。这只是一个简化的示例。因此,当我减小宽度时,我的用户控件需要垂直增长,以便显示标签中的整个文本。该标签不是自定义 winforms 标签,因为它呈现 HTML 内容。另外,我使用的是 .NET1,因此自动换行属性对我不可用
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 for 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需设置
Dock
或Anchor
您的 UserControl 中 Label 的属性,一切都会像魅力一样工作(您还可以在设计时检查正确的行为,只需调整 UserControl 的大小)。 ;-)Simply set the
Dock
orAnchor
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). ;-)它必须是
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 在调整大小时忽略控件高度更新”
你的意思是用户控件?我非常怀疑
确保您的事件通过所有正确的管道/控件,并在需要显示时执行失效(在 userContol 或/和嵌入表单中)。通常应该这样做。如果没有,请按照奥利弗的建议使用对接。
"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.