Hi Keith,
this code is taken probably from a class deriving from a control that
implements a OnTextChanged method (any Control).
the thing is that when deriving such a class that has one of its methods
marked as 'protected virtual' or transformed to 'protected override' you can
replace the implementation with your own (polymorphism),
on the other hand, if you are just consuming events (not deriving) you have
to provide handlers for the event.
usually, OnXXX methods are basicly methods that are called from within the
class to fire events. a basic implementation would look something like:
<code>
public event EventHandler MyEvent;
protected virtual void OnMyEvent(EventArgs e)
{
if (this.MyEvent!=null)
this.MyEvent(this, e);
}
private void EventLauncher1()
{
OnMyEvent(EventArgs.Empty);
}
private void EventLauncher2()
{
if (this.MyEvent!=null)
this.MyEvent(this, e);
}
</code>
this protected method (not only) saves the need to check if anyone has
signed on to the event like on the method EventLauncher2.
one other note - if you are subclassing and override that method, you have
to call the base implementation (like in your example -
base.OnTextChanged(e);). the reason is that if you do not, the event will
not fire (unless you fired it yourself). look at Control.OnTextChanged
documentation under Remarks/Notes to Inheritors
HTH
Picho
"Keith Smith" <ke*********@verizon.net> wrote in message
news:lDSPd.26968$uc.1882@trnddc09...
Could some one help me a little? I am trying to understand when/where you
would ever want to use "protected override..." code such as this. How is
this any different from creating a SelectedTextChanged type event?
This is an example I found on the web...
protected override void OnTextChanged(System.EventArgs e)
{
try
{
// Convert the text to a Double and determine
// if it is a negative number.
if(double.Parse(this.Text) < 0)
{
// If the number is negative, display it in Red.
this.ForeColor = Color.Red;
}
else
{
// If the number is not negative, display it in Black.
this.ForeColor = Color.Black;
}
}
catch
{
// If there is an error, display the
// text using the system colors.
this.ForeColor = SystemColors.ControlText;
}
base.OnTextChanged(e);
}