开发者

Custom Control Overriding Command Button

I am trying to create a custom command button that defaults width and height to specific settings. I have the following code:

public partial class myCommandButton : Button
{
    public magCommandButton()
    {
        InitializeComponent();

    }

    [DefaultValue(840)]
    public override int Width
    {
        get 
        {
            return base.Width;
        }
        set
        {
            base.Width = value;
        }
    }

    [DefaultValue(340)]
    public override int Height
    {
        get
        {
            return base.Height;
        }
        set
        {
            base.Height = value;
        }
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
    }
}

However, it won't compile because it tells me that I can not override Width or Height. Can anyone tell me if I'm approaching this wrongly, or if the开发者_开发百科re's a way around this?


Width and Height are not virtual members (check Control class in Reflector), so the simpliest way is to override DefaultSize property:

        protected override Size DefaultSize
        {
            get
            {
                return new Size(840, 340);
            }
        }

You can also use new modifier, but for this example I recommend to override DefaultSize. If you check Control class in Reflector you will see, that Height and Width use a lot of internal/private members. So I don't that implementing all those things is a good idea. Just use DefaultSize property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜