Peter said:
Not that I know of. All Component properties always have _some_ value,
and there's nothing to indicate to the Designer that the user should
always change the value from its default to something else.
I'm not sure why you want to put it in the OnParentChanged() method;
seems like it would be better to do it in OnLoad() or OnShown(), to give
client code the opportunity to change the values programmatically, in
the constructor after the call to InitializeComponent(), for example.
An OnLoad is what I originally intended to override, but I'm not finding
an OnLoad or OnShown in the list of overrideable methods I get when I
type "protected override void ". The list goes from OnLeave to
OnLocationChanged and from OnRightToLeftChanged to OnSizeChanged. Though
my experiencing in sub- or super-classing controls goes all the way back
to Win16, this is the first time I've written a custom control in .NET,
so I'm just first discovering the absence of these hooks and I'm
surprised by it.
The code for my class is below. Have I done something wrong? The
Persister and DataName properties both need non-empty values for the
control to make any sense; I'm going to be adding code to handle an
empty ConfigName as a special case, so the client developer won't have
to set that property unless he wants to.
public partial class PersistingTextBox : TextBox
{
private Persister persister;
private PersistHandler persistHandler;
public string ConfigName { get; set; }
public string DataName { get; set; }
public Persister Persister
{
get
{
return persister;
}
set
{
if (persister != null) persister.Persist -= persistHandler;
persister = value;
persister.Persist += persistHandler;
}
}
public PersistingTextBox()
{
InitializeComponent();
persistHandler = new PersistHandler(Persister_Persist);
}
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
if (!DesignMode)
{
this.Text = persister.GetValue(DataName);
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
void Persister_Persist()
{
persister.SetValue(DataName, this.Text);
}
}
There are lots of exceptions, and probably more than one that might be
appropriate in this situation. But I think I might use the
InvalidOperationException exception for this particular case. If you
like, you can sub-class that exception to provide a more detailed one of
your own specific type.
Thanks for the suggestion.