469,621 Members | 1,803 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

Inheriting a Form

I created a form, BaseForm, with a button as sort of a base class for some future windows. I added a button and put a simple MsgBox in the _click event that says "Hi from base" and I set the button's modifier property to "Protected."

Created DerivedForm inherited from BaseForm. Was able to go in and add a MsgBox to the _Click event for the button (as expected) that says "Hi from derived". When I run the app and click the button, I get both message boxes "Hi from base" then "Hi from derived"

My question is why can I not do this same thing with a Sub or Function? If I create BaseForm.TestThis(), I cannot seem to "extend" the code in DerivedForm.TestThis. Instead, I must overload or shadow it. Why the different behavior

Thank you. Bill

Nov 20 '05 #1
5 995
* "=?Utf-8?B?QmlsbHk=?=" <an*******@discussions.microsoft.com> scripsit:
I created a form, BaseForm, with a button as sort of a base class for some future windows. I added a button and put a simple MsgBox in the _click event that says "Hi from base" and I set the button's modifier property to "Protected."

Created DerivedForm inherited from BaseForm. Was able to go in and add a MsgBox to the _Click event for the button (as expected) that says "Hi from derived". When I run the app and click the button, I get both message boxes "Hi from base" then "Hi from derived".

My question is why can I not do this same thing with a Sub or Function? If I create BaseForm.TestThis(), I cannot seem to "extend" the code in DerivedForm.TestThis. Instead, I must overload or shadow it.


Just call 'MyBase.TestThis' in your 'TestThis' method of your derived form.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
Boy, that was an ugly post. How about some code snippets..

Public Class BaseFor
Inherits System.Windows.Forms.For
Protected Sub TestThis(
MsgBox("TestThis In base"
End Su

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProtected.Clic
MsgBox("Hi From Base"
End Su
End Clas

Public Class DerivedFor
Inherits MDIApplication.BaseFor

Private Sub btnProtected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProtected.Clic
MsgBox("Hi from derived"
End Su

Public Sub TestThis() ' INTELLISENSE ERROR HERE... TELLING ME I SHOULD SHADOW
MsgBox("In derived"
End Su

End Clas

I understand why TestThis needs to be shadowed, overloaded, or overridden, but don't understand why the _Click event of the inherited button isn't subject to the same rules? Why can I simply create an exact Click event in the derived class? Are there some special rules for object control events

Thanks

Bill

----- Billy wrote: ----

I created a form, BaseForm, with a button as sort of a base class for some future windows. I added a button and put a simple MsgBox in the _click event that says "Hi from base" and I set the button's modifier property to "Protected."

Created DerivedForm inherited from BaseForm. Was able to go in and add a MsgBox to the _Click event for the button (as expected) that says "Hi from derived". When I run the app and click the button, I get both message boxes "Hi from base" then "Hi from derived"

My question is why can I not do this same thing with a Sub or Function? If I create BaseForm.TestThis(), I cannot seem to "extend" the code in DerivedForm.TestThis. Instead, I must overload or shadow it. Why the different behavior

Thank you. Bill

Nov 20 '05 #3
Thank you. Per my post with code examples, I understand the need to qualify the derived TestThis sub, but can't figure out why the _Click event isn't under the same set of rules

Bill

----- Herfried K. Wagner [MVP] wrote: ----

* "=?Utf-8?B?QmlsbHk=?=" <an*******@discussions.microsoft.com> scripsit
I created a form, BaseForm, with a button as sort of a base class for some future windows. I added a button and put a simple MsgBox in the _click event that says "Hi from base" and I set the button's modifier property to "Protected."
Created DerivedForm inherited from BaseForm. Was able to go in and add a MsgBox to the _Click event for the button (as expected) that says "Hi from derived". When I run the app and click the button, I get both message boxes "Hi from base" then "Hi from derived"
My question is why can I not do this same thing with a Sub or Function? If I create BaseForm.TestThis(), I cannot seem to "extend" the code in DerivedForm.TestThis. Instead, I must overload or shadow it


Just call 'MyBase.TestThis' in your 'TestThis' method of your derived form

--
Herfried K. Wagner [MVP
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
* "=?Utf-8?B?QmlsbHk=?=" <an*******@discussions.microsoft.com> scripsit:
Thank you. Per my post with code examples, I understand the need to
qualify the derived TestThis sub, but can't figure out why the _Click
event isn't under the same set of rules?


That's because en event is similar to a list of function pointers. The
base class adds a handler to this list, and the subclass adds one too.
When raising the event, all the functions that are in the list are
called. You can manually remove the base class' handler by calling
'RemoveHandler', but this will only work if the handler can be accessed
from within the subclass.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
Excellent explanation! Thank you

Bill

----- Herfried K. Wagner [MVP] wrote: ----

That's because en event is similar to a list of function pointers. Th
base class adds a handler to this list, and the subclass adds one too
When raising the event, all the functions that are in the list ar
called. You can manually remove the base class' handler by callin
'RemoveHandler', but this will only work if the handler can be accesse
from within the subclass

Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Simon | last post: by
3 posts views Thread by YYZ | last post: by
2 posts views Thread by YYZ | last post: by
4 posts views Thread by elziko | last post: by
3 posts views Thread by Geraldine Hobley | last post: by
3 posts views Thread by Roy Soltoff | last post: by
1 post views Thread by BillE | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.