protected override question

  • Thread starter Thread starter Keith Smith
  • Start date Start date
K

Keith Smith

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);
}
 
OnTextChanged is raised directly by the TextChanged event. The
base.OnTextChanged() method is where the delegate-based event handlers are
called, along with whatever other functionality occurs there. As you can
see, in your derived control, you have two means by which you can respond to
the TextChanged event: delegate-based or direct without a delegate.

The delegate-based event handlers are the means by which consumers of your
control would connect to the control's TextChanged event. The protected
override OnTextChanged() method is how controls that inherit from the base
would connect to the TextChanged event.

The advantage to doing it this way is that the functionality you put in the
OnTextChanged() method override applies to all instances of your derived
control while the delegate-based event handlers will happen only for those
registered delegates that call your delegate method.

HTH

DalePres
MCAD, MCDBA, MCSE
 
Keith said:
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);
}

Well in this case, its the difference between changing the behaviour of
a control in a way that you can then reuse across projects and
responding to an event to provide a specific behaviour.

You could indeed simply provide an event handler for the TextChanged
event and provide this same functionality, but this would then only be
applicable in the project (in fact most likely just the form) that the
control is being used in. What is happening in your example snippet is
that the control is being extended to contain this behaviour in such a
way that you will get the desired behaviour everywhere you use the
extended control.

Regards Tim.
 
You'd do this if you were subclassing a control. If you created a
specialised type of TextBox, for instance, you'd use code like this to add
custom behaviour to your new control.
hth.
--p
 
The advantage to doing it this way is that the functionality you put in
the OnTextChanged() method override applies to all instances of your
derived control while the delegate-based event handlers will happen only
for those registered delegates that call your delegate method.

How do you associate the method with a particular control? For example, how
would C# associate the given example with, for example, textBox1?
 
You can't really do it the way you ask. To handle an event on a instance
you have to use a delegate.

When you override a method, it is in the context of the current class. If
you wish to create a version of a TextBox that implements special handling
via the OnTextChanged override, then create a new control, inherited from
TextBox, and then within the class definition of your new control, you would
override the OnTextChanged method.

public class MyTextBox : System.Windows.Forms.TextBox

{

public MyTextBox()

{

}

protected override void OnTextChanged(EventArgs e)

{

// Add code to do stuff here. For instance, if MyTextBox is a

// specialized textbox that makes every other character upper case,

// then here would be the place to make that happen.

base.OnTextChanged (e);

}

}


Now, if you create textBox1 as an instance of your new control, rather than
of the base TextBox class that it is now, your new textBox1 will execute the
code in the override for OnTextChanged when expected.

DalePres
 
DalePres said:
OnTextChanged is raised directly by the TextChanged event. The
base.OnTextChanged() method is where the delegate-based event handlers are
called

Slight mistype on the first sentence there I think ;D

The TextChanged event is raised directly by the OnTextChanged method
 
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
 
Back
Top