WinForms:右对齐标签自动调整大小
现在我澄清这可能是一个重复的问题:WinForms 中的右对齐标签 但没有一个答案令我满意。
问题很简单:
我有一个右对齐的标签,并将自动大小设置为 true。预期的行为是,当文本增加时,右坐标保持不变。但事实并非如此。左边的坐标是保持不变的。
我的应用程序有点小,所以我不想开始将控件放入面板等。所以我尝试了所有仅涉及标签属性的解决方案。唯一有效的方法是将 autosize 设置为 false 并放大它。 (已接受问题 N°1692383 的解决方案)。但实在是太丑了!我真的很想避免这种情况。
还有其他可能的解决方案吗?
Right now I clarify this may be a duplicated question from: Right-aligned labels in WinForms
But non of the answers satisfied me.
The problem is very simple:
I have a right aligned label with autosize set to true. The expected behavior is that when the text is increased the right coordinate remain unchanged. But that is not what it happens. The left coordinate is the one which remains unchanged.
My app is kind of small, so I don't want to start putting controls into panels and so. So I've tried all solutions that involved ONLY label properties. The only one which worked is to set autosize to false and over-size it. (Accepted solution of question Nº1692383). But it is really ugly! I'd really like to avoid that.
Any other possible solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种解决方案是在表单构造函数中捕获标签的右边距,并在标签
SizeChanged
事件中,根据初始右边距、标签的当前Width
重置位置以及标签 Parent 的当前宽度
。这还假设标签固定在右侧以处理表单大小调整。
One solution would be to capture the label's right margin in the form constructor, and in the label
SizeChanged
event, reset the location based on the initial right margin, the label's currentWidth
and the label Parent's currentWidth
.This also assumes the label is anchored on the right to handle form resizing.