Inheriting a Form

G

Guest

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
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?QmlsbHk=?= said:
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.
 
G

Guest

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
 
G

Guest

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=?= said:
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."

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

Herfried K. Wagner [MVP]

* "=?Utf-8?B?QmlsbHk=?= said:
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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top