当我将此控件放在窗体上时,更改其大小和位置,保存并关闭窗体。打开后位置和大小都不一样,但是在
.Designer.cs
里面就是我设置的样子
我找不到解决这个问题的方法,甚至没有人提到它。
这是我使用的自定义控件的一个简单示例:
[Designer(typeof(myControlDesigner1))]
public partial class UserControl1 : UserControl
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TypeConverter(typeof(Panel))]
[MergableProperty(false)]
public System.Windows.Forms.Panel Panel
{
get
{
return pnlWorkingArea;
}
set
{
pnlWorkingArea = value;
}
}
public UserControl1()
{
InitializeComponent();
}
}
public class myControlDesigner1 : ControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
UserControl1 bc = component as UserControl1;
EnableDesignMode(bc.Panel, "MyPanel");
}
}
是的我现在可以重现你的问题,那是因为面板在用户控件中,它们是作为一个整体添加到表单中的,这意味着面板的位置是相对于用户控件的,所以如果你设置面板的位置是(x, y),那么当你重新打开窗体时,面板的实际位置将是(usercontrol.location.X+x,usercontrol.location.Y+y)。
如果将窗体中usercontrol的位置设置为(0, 0),你会发现没有任何问题,请试一试。
如果不想设置usercontrol的location为(0, 0),作为替代方案,可以在Form_Load事件中加入如下代码,这样location就是你在窗体运行时设置的位置:
private void Form1_Load(object sender, EventArgs e)
{
this.userControl11.Panel.Location = new Point(userControl11.Panel.Location.X - userControl11.Location.X, userControl11.Panel.Location.Y - userControl11.Location.Y);
}