C# 标签属性在调整大小时不会更新

发布于 2024-09-06 04:01:23 字数 457 浏览 2 评论 0原文

我最近在实习中开始熟悉 Visual Studio 2010 和 C#。 C# 不包含内置的 InputBox 函数,因此我制作了自己的表单,其中包含一个文本框、两个按钮和一个简单的标签。

我设置了一个函数,允许程序员以常规格式(用户通过文本框输入输入)或是/否格式(其中表单仅显示问题以及是和否按钮)调用表单。

当我切换到是/否格式时,我想以编程方式将标签居中。我一直在使用代码:

labelNote.Left = inputBox.Left + (inputBox.Width / 2) - (labelNote.Width / 2);

这应该将注释的中心放在表单的中心。但是,如果标签的内容发生更改(使新标签更长或更短),则属性不会更新以反映新的大小。除非包含原始文本,否则它不会居中。有什么办法可以强制更新吗?我预见这将成为未来定位对象以实现可扩展性的问题。

谢谢您的宝贵时间

I recently started getting acquainted with Visual Studio 2010 and C# for an internship. C# doesn't include a built-in InputBox function, so I made my own form, with a text box, two buttons and a simple label.

I have a function set up to allow the programmer to call the form in regular format (where the user enters input via the textbox) or yes/no format (where the form just displays a question and the yes and no buttons).

When I switch over to yes/no format, I want to center the label programmatically. I've been using the code:

labelNote.Left = inputBox.Left + (inputBox.Width / 2) - (labelNote.Width / 2);

This should put the center of the note in the center of the form. However, if the contents of the label change (making the new label longer or shorter) the properties don't update to reflect the new size. It won't center unless it includes the original text. Is there some way to force an update? I foresee this becoming a problem with positioning objects for scalability in the future.

Thank you for your time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沩ん囻菔务 2024-09-13 04:01:23

我假设您在动作侦听器中具有大小调整功能。特别是表单的调整大小操作侦听器。然后,每当调整表单大小时,都会调用它并调用所有代码。然后,要从其他地方强制更新,您只需调用动作侦听器即可。

动作监听器:

Private Sub formName_Resize(ByVal sender As Object, ByVal e As  EventArgs) Handles MyBase.Resize

调用动作监听器:

formName_Resize(sender, e)

I'm assuming you have the sizing within an actionlistener. Specifically the forms' resize action listener. Then whenever the form is resized it is called and all of you code is recalled. Then to force an update from somewhere else you just have to call the actionlistener.

Actionlistener:

Private Sub formName_Resize(ByVal sender As Object, ByVal e As  EventArgs) Handles MyBase.Resize

Calls actionlistener:

formName_Resize(sender, e)
烟凡古楼 2024-09-13 04:01:23

好吧,您可以将一个事件附加到Label.TextChanged。坦率地说,最好更改 TextAlign 或类似的内容:尝试以声明方式执行布局,而不是通过代码显式执行。这往往会让事情变得更好。

我找到了 [TableLayoutPanel]1 控制要相当容易使用 - 大多数时候(偶尔会很痛苦)。

Well, you could attach an event to Label.TextChanged. Frankly it would be better to change the TextAlign or something like that though: try to perform the layout in a declarative fashion instead of doing it explicitly through code. That tends to make things work rather better.

I've found the [TableLayoutPanel]1 control to be reasonably easy to work with - most of the time (and occasionally a complete pain).

GRAY°灰色天空 2024-09-13 04:01:23

事实证明,我犯了一个愚蠢的错误(这是我调试时的常见主题。真正的小东西在很长一段时间内都不会被注意到)。

标签大小调整不是问题。问题在于我更改标签内容然后调用函数来计算其新位置的顺序。我首先调用位置计算,因此它根据旧内容找到了标签居中的位置。我没有注意到这么久,因为文本正在正确变化。我理所当然地认为这些函数是按正确的顺序调用的。

因此,当有疑问时,请检查编写代码的顺序。不管怎样,谢谢大家的帮助。我最终发现了一些可以适用于其他场景的巧妙的东西(例如 Graphics 类中的 MeasureString 函数)。

It turns out that I made a stupid mistake (a common theme for me in debugging. The really small stuff goes unnoticed for the longest amount of time).

The label resizing was not the issue. The issue was the order in which I changed the contents of the label and then called the function to calculate its new location. I was calling the location calculation first, so it found where to center the label based on the old contents. I didn't notice for so long, because the text was changing properly. I took it for granted that the functions were being called in the correct order.

So, when it doubt, check the order in which you're writing your code. Thanks for you help anyway, everyone. I ended up finding out some neat things that could be applicable to other scenarios (such as the MeasureString function in the Graphics class).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文