views:

174

answers:

1

Morning all,

I've created a custom control with an image property. That image property is a get/set to a private Image variable.

Can anyone tell me how I enable that get/set to clear the property from the designer?

I.e. if I add an image to a standard PictureBox, I can hit Del to clear the image from the PictureBox. How can I replicate this behaviour on my own custom control?

+3  A: 

At the simplest level, DefaultValueAttribute should do the job:

private Bitmap bmp;
[DefaultValue(null)]
public Bitmap Bar {
    get { return bmp; }
    set { bmp = value; }
}

For more complex scenarios, you might want to try adding a Reset method; for example:

using System;
using System.Drawing;
using System.Windows.Forms;
class Foo {
    private Bitmap bmp;
    public Bitmap Bar {
        get { return bmp; }
        set { bmp = value; }
    }
    private void ResetBar() { bmp = null; }
    private bool ShouldSerializeBar() { return bmp != null; }
}
static class Program {
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Form form = new Form();
        PropertyGrid grid = new PropertyGrid();
        grid.Dock = DockStyle.Fill;
        grid.SelectedObject = new Foo();
        form.Controls.Add(grid);
        Application.Run(form);
    }
}
Marc Gravell