即使调整了表单大小,如何使标签保持居中 [英] How do I keep my labels centered even if form is resized

查看:123
本文介绍了即使调整了表单大小,如何使标签保持居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个利用文本框和标签的软件。如果调整窗体大小,我需要调整文本框的大小;我希望标签留在文本框的中心



使文本框调整大小并不难,我只是将它们锚定到左,右和上。





这是我遇到的问题:



我以为我可以通过对文本框应用相同的锚定设置让他们工作,但这不起作用。





表格看起来不错第一次加载。看到这里^



但是当你调整表格大小时,事情就出错了。 点击此处查看



我该怎么做?

先谢谢!! :)

解决方案

2个选择:



1:为你的标签保持左右锚,但更改以下属性:

- AutoSize = false

- TextAlign = TopCenter



2:锚定标签仅限于顶部(向左和向右移除)



第一种方法将更改标签大小,但文本将在调整大小的控件中居中。不方便:当表格缩小时,标签会减少,文字可能会消失。



第二种方法会移动标签,在左边的尺寸上增加相同的距离尺寸合适。如果标签首先居中,它将保持居中。但是如果它在左边(或右边)对齐,它会在窗体增加时移动,或者在窗体缩小时消失。



可以使用相同的行为verticaly当没有锚定到顶部和底部时。



它可以用于任何.Net控件(不仅仅是标签)



编辑:此解决方案适用于WinForms,对于WPF,只需将Horizo​​ntalAlignment属性更改为Center


假设您正在为WinForms开发,它只需要一个bit o'数字运算以重新对齐标签。在我的头顶(和咖啡前),这应该这样做:

 myLabel.X = myTextBox.Left +(myTextBox.Width  -  myLabel.Width) / 2; 

在表单中调用调整大小处理程序。



/ ravi


I'm creating a software which utilizes textboxes and labels. I need the textboxes to resize if the form is resized; and I want the labels to stay at the center of the textboxes

Making the textboxes resize wasn't hard, I just anchored them to the left, right and top.


This is the problem I'm having:

I thought I could get them to work by applying the same anchoring settings to the textbox but that's not working.


It looks okay when the form first loads. See here^

But when you resize the forms, things go wrong. Click here to see it

How do I do this?
Thanks in advance!! :)

解决方案

2 choices :

1 : keep anchor to left and right for your label but change the following properties :
- AutoSize = false
- TextAlign = TopCenter

2 : anchor the label only to the top (remove left and right)

First method will change the label size but the text will be centered in the resized control. Inconvenient : when the form is reduced, the label will be reduced and the text may disappear.

Second method will move the label adding the same distance on its left size and on its right size. if the label is first centered, it will remain centered. but if it is align on the left (or right) it will move when the form increase or may disappears when the form is reduced.

Same behaviour can be used verticaly when not anchord to top and bottom.

And it can be used for any .Net Controls (not only labels)

Edit: This solution is for WinForms, for WPF, just change the "HorizontalAlignment" property to "Center"


Assuming you're developing for WinForms, it just takes a bit o' number crunching to re-align the labels. Off the top of my head (and before coffee), this should do it:

myLabel.X = myTextBox.Left + (myTextBox.Width - myLabel.Width)/2;

Call in your form's Resize handler.

/ravi


这篇关于即使调整了表单大小,如何使标签保持居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆