HOW: Inherited forms and OnClick

K

Kenneth Bohman

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
 
V

Vijayakrishna Pondala

In the inherited form, don't add the event delegate to the OnClick event.
 
K

Kenneth Bohman

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
 
L

Lorne Smith

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
 
K

Kenneth Bohman

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
 
L

Lorne Smith

Kenneth Bohman said:
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
 
K

Kenneth Bohman

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

Thanks again,

Kenneth
Lorne Smith said:
Kenneth Bohman said:
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
 
L

Lorne Smith

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 said:
I'll try that. No example, just the description. I'm using 1.0, maybe you
are using 2003.

Thanks again,

Kenneth
Lorne Smith said:
Kenneth Bohman said:
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
 

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