简体   繁体   English

将控件添加到标签页后,其位置已更改

[英]Location of controls got changed after adding it to a tabpage

I have winform application which uses a tabPage, and I dynamically added some controls into the page. 我有一个使用tabPage的winform应用程序,并且在页面中动态添加了一些控件。 However the location of the controls got changed after then. 但是,此后控件的位置发生了变化。 After some tweaking, it seems the location of the controls are correct, but the size of some controls are not correct again. 进行一些调整后,似乎控件的位置正确,但是某些控件的大小又不正确。 Please help!!! 请帮忙!!! thanks 谢谢

The code snippet like this: 这样的代码片段:

foreach (Control c in controls)
{
    Point oldLocation = c.Location;
    this.SuspendLayout();                                          
    this.Controls.Add(c);                        
    c.Location = oldLocation;                       
    c.BringToFront();
    this.ResumeLayout(false);
    this.PerformLayout();
}

I did not describe this in great detail probably. 我可能没有对此进行详细描述。 What I need is to have a floating tab page; 我需要的是一个浮动标签页; so I ended up with keeping another form, and switch on/hide the form and the tab page when switch between floating and docking mode. 因此,我最终保留了另一种形式,并在浮动和停靠模式之间切换时打开/隐藏了表单和标签页。

To achieve this, I need to copy/remove all the controls from either the form and the page. 为此,我需要从表单和页面中复制/删除所有控件。 It turns out that the control location /size will be changed between the remove/copy. 原来,控件的位置/大小将在删除/复制之间进行更改。

The workaround I ended up with is to store the original location/size before I do the remove and copy, and reset the control's location and size afterward. 我最终得到的解决方法是在执行删除和复制之前存储原始位置/大小,然后再重置控件的位置和大小。 It sounds a pretty lame solution, but I cannot find any better solution now. 听起来不错的解决方案,但我现在找不到更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM