Adding .NET Control Properties
by Phil Wright


Listing One

public class MyControl : UserControl
{
    private string _title;

    [Category("Property Changed")]
    [Description("Fired when title is changed.")]
    public event EventHandler TitleChanged;

    public MyControl()
    {
        ResetTitle();
    }
    [Category("Appearance")]
    [Description("Text displayed in the control title.")]
    [DefaultValue("MyDocument")]
    public string Title
    {
        get { return _title; }
        set
        {
            if (_title != value)
            {
                _title = value;
                OnTitleChanged(EventArgs.Empty);
            }
        }
    }
    public void ResetTitle()
    {
        Title = "MyDocument";
    }
    protected virtual void OnTitleChanged(EventArgs e)
    {
        if (TitleChanged != null)
            TitleChanged(this, e);
    }
}


Listing Two

public class MyControl : UserControl, ISupportInitialize
{
    private bool _auto;
    private bool _mode;
    private bool _defaultAuto;
    public MyControl()
    {
        BeginInit();
        ResetAuto();
        ResetMode();
        EndInit();
    }
    public void BeginInit()
    {
    _defaultAuto = false;
    }
    public void EndInit()
    {
        if (_defaultAuto)
            ResetAuto();
    }
    [Category("Behavior")]
    [Description("Mode of operation.)]
    [DefaultValue(true)]
    public bool Mode
    {
        get { return _mode; }
        set
        {
            if (_mode != value)
            {
                _mode = value;
                Auto = !value;
    }
    }
        }
    public void ResetMode()
    {
    Mode = true;
}
    [Category("Behavior")]
    [Description("Automatic reset from Mode.)]
    public bool Auto
    {
        get { return _auto; }
        set
        {
            if (_auto != value)
                _auto = value;

            _defaultAuto = false;
           }
    }
    public void ResetAuto()
    {
        if (Mode)
            Auto = false;
        else
            Auto = true;
        _defaultAuto = true;
    }
    private bool ShouldSerializeAuto()
    {
    return (Auto == Mode);
}
    }

