By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

User control question

P: n/a
I have created a VB.NET User Control which inherits from TextBox. I
have a general question regarding how functionality should be
overwritten:

I can either write a method which handles an event:

function MyControl_Enter(...) handles MyControl.Enter

or I can overwrite the relevant virtual method:

function overwrites onEnter(...)

Are these two ways of doing the same thing? When should I use the
first approach, and when the other?

Thanks for helping me understand this.
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
When you create an event handler, you are 'Creating' a method which handles
an event, in otherwords you register this method address to be fired when a
certain event occurs. You are not overriding anything and you dont need to.

What changes are you trying to make ?
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"James Ramaley" <jr******@iname.com> wrote in message
news:f8**************************@posting.google.c om...
I have created a VB.NET User Control which inherits from TextBox. I
have a general question regarding how functionality should be
overwritten:

I can either write a method which handles an event:

function MyControl_Enter(...) handles MyControl.Enter

or I can overwrite the relevant virtual method:

function overwrites onEnter(...)

Are these two ways of doing the same thing? When should I use the
first approach, and when the other?

Thanks for helping me understand this.

Nov 20 '05 #2

P: n/a
James,
Is MyControl the name of the user control?

In addition to what OHM stated.

..NET does not allow a derived class from invoking a base event. Hence if you
wanted to raise the Paint event in your code you would not be able to. Yes
Yes, you should really use Invalidate & Update or Refresh on a form to cause
the Paint event to be raised, but bear with me. ;-)

So the pattern in .NET is to have an event MyEvent and an overridable method
OnMyEvent. The OnMyEvent when called will raise the MyEvent of the base.
Because its overridable derived classes can choose to do something before or
after calling the base class's OnMyEvent... Like replacing the event args
with a class derived from the normal event args adding addition
information...

Also, overriding a method is generally quicker than handling an event, at
least cleaner. So overriding the OnPaint method should perform better than
handling the Paint event, especially if there are other handlers of said
event...

The most important thing about overriding the OnPaint method (especially the
OnPaint) is to call the base class's OnPaint.

I think of it this way:
The OnPaint method is for notification internal to the object (the class,
classes I derive from, and classes that derive from me).
The Paint event is for notification external to the object (other objects).

In your example, I would override the OnEnter method, being sure to call
MyBase.OnEnter within my MyControl user control.

For example, when I design a form I normally override the OnLoad method,
instead of handling the MyBase.Load event (as the designer would have you
do).

Hope this helps
Jay
"James Ramaley" <jr******@iname.com> wrote in message
news:f8**************************@posting.google.c om...
I have created a VB.NET User Control which inherits from TextBox. I
have a general question regarding how functionality should be
overwritten:

I can either write a method which handles an event:

function MyControl_Enter(...) handles MyControl.Enter

or I can overwrite the relevant virtual method:

function overwrites onEnter(...)

Are these two ways of doing the same thing? When should I use the
first approach, and when the other?

Thanks for helping me understand this.

Nov 20 '05 #3

P: n/a
>
Hope this helps
Jay


Yes Jay, it does help a lot.

thanks
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.