将锚属性与动态添加的控件一起使用 [英] Using anchor property with dynamically added controls

查看:25
本文介绍了将锚属性与动态添加的控件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时动态地向表单添加一些文本框.一切正常,即文本框对齐、固定并自动调整大小直到表单最大化.在最大化表单时,文本框被添加到相同的位置,而表单不是> 最大化.这会导致文本框错位.

I'm adding some textboxes to a form dynamically at runtime. Everything works fine i.e. the textboxes are aligned, anchored and automatically resizes until the form is maximized. On maximizing the form, the textboxes are added to the same location while the form was not maximized. This causes a misalignment of the textboxes.

如何在窗口状态正常和最大化的情况下确保所有文本框都在相同的位置和相同的大小?

How can I ensure that all the textboxes are at the same location and of the same size both while the windowstate is normal as well as maximized?

顺便说一句,我正在使用 C#

Btw I'm using C#

flowlayoutpanel 在这里有用吗?

Would a flowlayoutpanel be useful here?

推荐答案

这是一个很老的问题,但也许我可以回答.

It's a quite old question, but maybe i'm able to answer it.

阅读您所有的评论后,我想我可以将您的问题总结为:

After reading all your comments, i think i can summarize your problem to this:

  • 您有一个特定大小的表单,并在运行时在特定位置添加一些控件,并将锚点设置为 Top |没错.
  • 如果您只是显示表单并让控件出现,则一切正常
  • 如果您最大化表单(或更改表单的大小),您的控件将不会再出现在您想要的正确位置.

要解决这个问题,您可以尝试一些不同的方法:

To get rid of this problem you can try some different approaches:

  1. 使用FlowLayoutPanel,注意FlowDirection 可能只是事先创建所有需要的控件,然后只需切换可见状态.
  2. 为新创建的控件的位置使用正确的值.
  1. Use a FlowLayoutPanel, take care for the FlowDirection and maybe just create all your needed controls beforehand and just toggle the visible state.
  2. Use correct values for the location of your newly created controls.

第二点是你的错误(我认为).如果您的表单具有原始大小,您就找到了计算控件位置的方法.如果表单的大小发生变化(例如最大化),要获得正确的位置,您必须考虑几个因素.

The second point is the error you have (i think). You found someway to calculate the location of your control if your form has it's original size. To get the correct position if the size of the form has changed (e.g. maximized) you have to consider several factors.

  • 从默认尺寸到当前尺寸的增量值.
  • 您希望在控件上设置的锚点.

在您的情况下,您想放置一个锚定的控件 Top |对,但位置由Top | 设置左.在这种情况下,您必须计算 control.location.x 和 form.width 在其默认大小中的差异.然后你把这个差值从表单当前宽度中减去.现在您可以将您的控件放在这个位置(因为 Top 永远不会通过调整大小而改变).如果你在 Bottom | 上有一个锚点对你必须用 control.location.y 和 form.height 来计算.

In your case you'd like to put a control which is anchored Top | Right, but the location is set by Top | Left. In that case you have to calculate the difference between the control.location.x and the form.width in it's default size. Then you take this difference and subtract it from the form current width. Now you can place your control at this position (cause Top never changes through a resizing). If you have a Anchor at Bottom | Right you have to calculate the same with control.location.y and form.height.

如果没有锚点的行为和计算,对于Top |底部Left |右侧设置留给读者作为练习.;-)

The behaviour and calculation if no anchor, for Top | Bottom or Left | Right are set is left as an exercise to the reader. ;-)

最后但并非最不重要的还有另一种让您控制在正确位置的方法:

Last but not least there is also another hacky way to get your control at the right position:

  • 如果您想在某处放置新控件,请将 Form.Visible 更改为 false
  • 保存表单状态、大小和位置
  • 将它们更改为您的默认值
  • 添加您想要的控件
  • 恢复以前保存的值
  • 使表单再次可见.

这篇关于将锚属性与动态添加的控件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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