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

Re: Inherited forms and OnClick by Vijayakrishna

Vijayakrishna
Wed Jan 07 01:40:19 CST 2004

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
>
>
>
>
>



Re: Inherited forms and OnClick by Kenneth

Kenneth
Wed Jan 07 02:33:20 CST 2004

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
> >
> >
> >
> >
> >
>
>



Re: Inherited forms and OnClick by Lorne

Lorne
Wed Jan 07 06:02:59 CST 2004

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
> > >
> > >
> > >
> > >
> > >
> >
> >
>
>



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
>> > >
>> > >
>> > >
>> > >
>> > >
>> >
>> >
>>
>>
>
>
>.
>

Re: Inherited forms and OnClick by Lorne

Lorne
Wed Jan 07 12:55:31 CST 2004


"Kenneth Bohman" <developer@itinitiative.co.uk> wrote in message
news:099401c3d51a$6a3c2b60$a401280a@phx.gbl...
> 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
>

I found the item you refer to above on MSDN and it had the following example
attached, did it not appear on yours?

\\\
The following code example demonstrates overriding the OnClick method in a
derived class. To run the example, paste the following code after a form
class, in the same file. Add a textbox of type SingleClickTextBox to the
form.
[Visual Basic]
' This is a custom TextBox control that overrides the OnClick method
' to allow one-click selection of the text in the text box.

Public Class SingleClickTextBox
Inherits TextBox

Protected Overrides Sub OnClick(ByVal e As EventArgs)
Me.SelectAll()
MyBase.OnClick(e)
End Sub

End Class
///

HTH

Lorne



Re: Inherited forms and OnClick by Kenneth

Kenneth
Wed Jan 07 17:13:46 CST 2004

I'll try that. No example, just the description. I'm using 1.0, maybe you
are using 2003.

Thanks again,

Kenneth
"Lorne Smith" <no@spam.com> skrev i meddelandet
news:OBjeQ$U1DHA.3656@TK2MSFTNGP11.phx.gbl...
>
> "Kenneth Bohman" <developer@itinitiative.co.uk> wrote in message
> news:099401c3d51a$6a3c2b60$a401280a@phx.gbl...
> > 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
> >
>
> I found the item you refer to above on MSDN and it had the following
example
> attached, did it not appear on yours?
>
> \\\
> The following code example demonstrates overriding the OnClick method in a
> derived class. To run the example, paste the following code after a form
> class, in the same file. Add a textbox of type SingleClickTextBox to the
> form.
> [Visual Basic]
> ' This is a custom TextBox control that overrides the OnClick method
> ' to allow one-click selection of the text in the text box.
>
> Public Class SingleClickTextBox
> Inherits TextBox
>
> Protected Overrides Sub OnClick(ByVal e As EventArgs)
> Me.SelectAll()
> MyBase.OnClick(e)
> End Sub
>
> End Class
> ///
>
> HTH
>
> Lorne
>
>



Re: Inherited forms and OnClick by Lorne

Lorne
Thu Jan 08 02:38:21 CST 2004

I am using 2003, yes, and the example I posted is from the 1.1
documentation, but I don't see anything there that wouldn't be compatible
with 1.0...

Still, glad to be of any help... :)

Lorne

"Kenneth Bohman" <developer@itinitiative.co.uk> wrote in message
news:ep3a3PX1DHA.1764@TK2MSFTNGP10.phx.gbl...
> I'll try that. No example, just the description. I'm using 1.0, maybe you
> are using 2003.
>
> Thanks again,
>
> Kenneth
> "Lorne Smith" <no@spam.com> skrev i meddelandet
> news:OBjeQ$U1DHA.3656@TK2MSFTNGP11.phx.gbl...
> >
> > "Kenneth Bohman" <developer@itinitiative.co.uk> wrote in message
> > news:099401c3d51a$6a3c2b60$a401280a@phx.gbl...
> > > 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
> > >
> >
> > I found the item you refer to above on MSDN and it had the following
> example
> > attached, did it not appear on yours?
> >
> > \\\
> > The following code example demonstrates overriding the OnClick method in
a
> > derived class. To run the example, paste the following code after a form
> > class, in the same file. Add a textbox of type SingleClickTextBox to the
> > form.
> > [Visual Basic]
> > ' This is a custom TextBox control that overrides the OnClick method
> > ' to allow one-click selection of the text in the text box.
> >
> > Public Class SingleClickTextBox
> > Inherits TextBox
> >
> > Protected Overrides Sub OnClick(ByVal e As EventArgs)
> > Me.SelectAll()
> > MyBase.OnClick(e)
> > End Sub
> >
> > End Class
> > ///
> >
> > HTH
> >
> > Lorne
> >
> >
>
>