Re: Inherited forms and OnClick by Kenneth
Kenneth
Wed Jan 07 06:33:12 CST 2004
Thanks Lorne,
Makes perfect sense to me! Now the only thing is the
OnClick event, which I understand is the preferred method.
MSDN seem to agree
"The OnClick method also allows derived classes to handle
the event without attaching a delegate. This is the
preferred technique for handling the event in a derived
class.
Notes to Inheritors: When overriding OnClick in a
derived class, be sure to call the base class's OnClick
method so that registered delegates receive the event."
But I find no examples. Have you seen one?
Kenneth
>-----Original Message-----
>Hi Kenneth,
>
>What happens is that as both the base form and the
overridden form are
>handling the click event of the button, both events are
fired, but as you
>have overridden the base method, it only actually
executes the code within
>the overridden event. If you remove the Handles
directive from the
>overriden event declaration, the code will only execute
once. You would
>then have the following in the forms...
>
>Base Form
>\\\
>Public Overridable Sub btnAdd_Click(ByVal sender As
System.Object, ByVal e
>As System.EventArgs) Handles btnAdd.Click
>
>DoSomething
>
>End Sub
>///
>
>Inherited form
>\\\
>Public Overrides Sub btnAdd_Click(ByVal sender As
System.Object, ByVal e
>As System.EventArgs)
>
>DoSomethingLessBoringInstead
>
>End Sub
>///
>
>Clicking on the button in the inherited form will call
the event declaration
>in the base form which will pass it up to the overridden
event in the
>inherited form and only that code will be executed.
>
>My explanation may be a little lacking, I've only been
working with .NET for
>a few weeks (and not a lot over christmas!), but I'm
reasonably sure that's
>correct
>
>HTH
>
>Lorne
>
>
>
>"Kenneth Bohman" <developer@itinitiative.co.uk> wrote in
message
>news:#1I6#jP1DHA.3140@tk2msftngp13.phx.gbl...
>> Hi Vijayakrishna,
>>
>> I've understood that. Do you have an example? Let's
say I have this in the
>> base form and I want to override it in the inheritied
form, what should it
>> look like in the base and the inherited form
respectively?
>>
>> Public Overridable Sub btnAdd_Click(ByVal sender As
System.Object, ByVal e
>> As System.EventArgs) Handles btnAdd.Click
>>
>> DoSomething
>>
>> End Sub
>>
>> "Vijayakrishna Pondala" <pvijkris@hotmail.com> skrev i
meddelandet
>> news:uHs%23BGP1DHA.2388@TK2MSFTNGP09.phx.gbl...
>> > In the inherited form, don't add the event delegate
to the OnClick
>event.
>> > "Kenneth Bohman" <developer@itinitiative.co.uk>
wrote in message
>> > news:O2dTlBP1DHA.1924@TK2MSFTNGP10.phx.gbl...
>> > > Armin wrote on the 1st of Jan as a reply to Lorne
Smith
>> > > "The designer creates the default procedure for
events. Event
>procedures
>> > are
>> > > usually not Public Overridable. IMO, you should
not change the
>signature
>> > in
>> > > the base class. If you want an overridable sub
handling the Click, add
>> an
>> > > Overridable OnButtonClick procedure called in the
event handler of the
>> > base
>> > > class. In the derived class, override
OnButtonClick".
>> > >
>> > > Question 1:
>> > > Can anyone give an example (VB.NET if possible) on
how to do that? I
>> can't
>> > > find any. Not even the MSDE example for OnClick is
about OnClick. I've
>> got
>> > a
>> > > copule of books on Windows Forms (many of the
good) and loads of
>online
>> > > articles, the authors avoid going into inherited
forms, which is a
>pity,
>> > > since quite many questions in this discussion
group seem to be about
>> just
>> > > that.
>> > >
>> > > Question 2:
>> > > I'm successfully using override, but now I get
confused:
>> > > Lorne Smith writes: "when I put code in the sub,
it was executed twice
>> > when
>> > > I ran the form. Now I realised this is because
both the base form and
>> the
>> > > inherited form are handling the event".
>> > > WHY does both the base form and the inherited form
execute the event?
>I
>> > > thought override meant that the overriding method
executes INSTEAD of
>> the
>> > > overridden, not that both are executed. Why would
I use the override
>> > keyword
>> > > if I want both to execute? But clearly Lorne is
right in that both are
>> > > executed. I've considered that an oddity, but it
is by-design then.
>> > >
>> > > Kind Regards,
>> > >
>> > > Kenneth Bohman
>> > >
>> > >
>> > >
>> > >
>> > >
>> >
>> >
>>
>>
>
>
>.
>