Patrik Lippojoki
Patrik Lippojoki

Reputation: 1643

Prevent control's Size-property to be set?

I'm trying to create a custom Panel class which wouldn't allow it's size to be set outside of the class. What I tried was this:

public class FixedPanel : Panel
{
    public override Size Size { get; private set; }
}

But I get an error saying that FixedPanel.Size.set: cannot override inherited member 'System.Windows.Forms.Control.Size.set' because it is not marked virtual, abstract, or override

Is there a way to prevent user from setting control's Size?


Edit: I came up with a cleaner solution that removes the need to set two properties separately, but I'm not sure if it's really a proper way either. Here's what I'd do:

private Size fixedSize;
private void SetFixedSize(Size size=null)
{
    if (size)
        this.fixedSize = size;

    if (this.Size != this.fixedSize)
        this.Size = this.fixedSize;
}

public FixedPanel()
{
    this.SizeChanged += SetFixedSize;
}

Upvotes: 2

Views: 512

Answers (1)

Janez Lukan
Janez Lukan

Reputation: 1449

Try with new:

public class FixedPanel : Panel
{
    public new Size Size { get; private set; }
}

Upvotes: 3

Related Questions