0

当我将此控件放在表单上时,更改它的大小和位置,保存并关闭表单。打开后,位置和大小都不一样了,但是在“.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");
    }
}
4

1 回答 1

0

是的,我现在可以重现您的问题,那是因为面板在用户控件内,它们作为一个整体添加到表单中,这意味着面板的位置是相对于用户控件的,所以如果您将面板的位置设置为 ( x, y),那么当您重新打开表单时,面板的实际位置将是 (usercontrol.location.X+x, usercontrol.location.Y+y)。

如果在表单中设置用户控件的位置为(0, 0),你会发现没有任何问题,请试一试。

如果您不想将用户控件的位置设置为 (0, 0),作为替代解决方案,您可以在 Form_Load 事件中添加以下代码,这样该位置将是您在运行窗体时设置的位置:

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);
}
于 2018-03-20T03:34:23.507 回答