473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

protected override question

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);
}

Nov 16 '05 #1
8 11772
Keith Smith wrote:
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.
Nov 16 '05 #2
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
"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);
}

Nov 16 '05 #3
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 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);
}

Nov 16 '05 #4
> 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?
Nov 16 '05 #5
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

"Keith Smith" <ke*********@verizon.net> wrote in message
news:PxTPd.18246$uc.1189@trnddc05...
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?

Nov 16 '05 #6
"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
news:uj**************@TK2MSFTNGP14.phx.gbl...
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
Nov 16 '05 #7
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);
}

Nov 16 '05 #8
Sean,

You're absolutely right!

Thanks,

Dale Preston

"Sean Hederman" <us***@blogentry.com> wrote in message
news:cu**********@ctb-nnrp2.saix.net...
"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
news:uj**************@TK2MSFTNGP14.phx.gbl...
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

Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dan Cimpoiesu | last post by:
Hello What does mean when a protected modifier is apllied to a class. I want to inherit a protected class defined in another class, I saw that the microsoft programmers achieved that. But I...
4
by: p988 | last post by:
using System; using System.Windows.Forms; using System.Drawing; class MyForm : Form { MyForm () { Text = "Windows Forms Demo"; }
1
by: Jax | last post by:
Here is the CodeDom code for the standard Dispose method of a windows form application. The problem I have is with the Attributes property, it isn't a collection so only one attribute can be...
7
by: TT (Tom Tempelaere) | last post by:
Hi there The line marked *** doesn't compile. I wonder why the designers of C# decided to disallow this. Rationale? Are there plans to change this? This feature forces me to make things public...
3
by: gordon | last post by:
can someone please explain what protected override void is ? what is it used for? thanks Doug
13
by: Clive Dixon | last post by:
I am refactoring some code to move some base classes into a separate assembly. One of these base classes has a member property which is 'protected internal'. However when I move these base classes...
5
by: =?Utf-8?B?V0o=?= | last post by:
If I have a class A that has a virtual public function DoSomething() Class A { public: virtual void DoSomething(); } then I override this function to protected is class B Class B: public...
6
by: REH | last post by:
Can someone please tell me what is wrong with this snippet of code? GCC is telling me that "foo2 is protected within this context" (that I cannot use it with BBB). class AAA { protected:...
7
by: =?Utf-8?B?cmFtbzk5NDE=?= | last post by:
Hi, Is there any way disable using "protected override void OnPaint(..." method out of the assembly. I create usercontrol and i do not want user adds extra codes OnPaint method for securtity...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.