Possible to ignore base class event handler?

  • Thread starter Thread starter Cride
  • Start date Start date
C

Cride

Hello,

I have this form Form_Report_Vacations which is inherited from
Form_Report_Shifts.
Form_Report_Shifts contains an event handler for a button named
cmd_Show.

In my inherited form i want to specify different behaviour for the
click event of the button.
I dont want the code that lies in the base class to execute at all.

I have done this in the base class event handler:

---------------------------------------------------------------------------------------------------------------
Private Sub cmd_Show_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmd_Show.Click

' Exit sub if not in base class.
If CType(sender, Control).FindForm.Name <> "Form_Report_Shifts" Then
Exit Sub
End If

' More code
....
....

End Sub
---------------------------------------------------------------------------------------------------------------

This certainly works but it looks a little bit ugly in my opinion.
Is there a better way than this to prevent execution of code in the
event handler of the base class?

- Christian
 
Hello,

I have this form Form_Report_Vacations which is inherited from
Form_Report_Shifts.
Form_Report_Shifts contains an event handler for a button named
cmd_Show.

In my inherited form i want to specify different behaviour for the
click event of the button.
I dont want the code that lies in the base class to execute at all.

I have done this in the base class event handler:

---------------------------------------------------------------------------------------------------------------
Private Sub cmd_Show_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmd_Show.Click

' Exit sub if not in base class.
If CType(sender, Control).FindForm.Name <> "Form_Report_Shifts" Then
Exit Sub
End If

' More code
...
...

End Sub
---------------------------------------------------------------------------------------------------------------

This certainly works but it looks a little bit ugly in my opinion.
Is there a better way than this to prevent execution of code in the
event handler of the base class?

- Christian

You need to mark your event handler method as protected overridable.
Then in the class that inherits your base page you override that
method and remove the MyBase.cmd_Show_Click() call and replace it with
your own code.

Let me know if you need a sample.

Thanks,

Seth Rowe [MVP]
 
You need to mark your event handler method as protected overridable.
Then in the class that inherits your base page you override that
method and remove the MyBase.cmd_Show_Click() call and replace it with
your own code.

Let me know if you need a sample.

Thanks,

Seth Rowe [MVP]

Hello,

Thanks for your answer.

I tried the thing you suggested and it worked. The code in the base
class event handler was not called.
The event handler in the inherited class, however, seemed to be firing
twice.
This was solved by removing the handler implicitly in the
Form_Child.Designer.vb file
and then adding it again.

I don't know if this is something you always have to do
as to prevent the event handler in the inherited class to be fired
twice.

Thanks.

- Christian
 
Hello,

Thanks for your answer.

I tried the thing you suggested and it worked. The code in the base
class event handler was not called.
The event handler in the inherited class, however, seemed to be firing
twice.
This was solved by removing the handler implicitly in the
Form_Child.Designer.vb file
and then adding it again.

I don't know if this is something you always have to do
as to prevent the event handler in the inherited class to be fired
twice.

Thanks.

- Christian

Christian,

I think I would organize the code slightly differently. In the base
form, I would do something more like:

Public Class BaseForm
Inherits System.Windows.Forms.Form

....

Private Sub cmd_Show_Click (ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmd_Show.Click

OnCmdShowClick ()

End Sub

Protected Overridable Sub OnCmdShowClick ()
' Default base implementation
End Sub
End Sub

Public Class Derived
Inherits BaseForm

Protected Overrides Sub OnCmdShowClick()
' derived class implementation
End Sub
End Class

I think this will solve your problem of the double event.
 
Christian,

I think I would organize the code slightly differently. In the base
form, I would do something more like:

Public Class BaseForm
Inherits System.Windows.Forms.Form

....

Private Sub cmd_Show_Click (ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmd_Show.Click

OnCmdShowClick ()

End Sub

Protected Overridable Sub OnCmdShowClick ()
' Default base implementation
End Sub
End Sub

Public Class Derived
Inherits BaseForm

Protected Overrides Sub OnCmdShowClick()
' derived class implementation
End Sub
End Class

I think this will solve your problem of the double event.

I tried this approach, thus removing the call to RemoveHandler, but
the event is still fireing twice.

The only thing that has worked yet is the following
 
I tried this approach, thus removing the call to RemoveHandler, but
the event is still fireing twice.

Hmmm... I don't get that behavior here. Are you sure you tried the
above method (or similoar)? Can you post a more complete listing of the
derived forms code?
 
Hmmm... I don't get that behavior here. Are you sure you tried the
above method (or similoar)? Can you post a more complete listing of the
derived forms code?

Ok,

I made a simple test with two forms. Form_Base and Form_Child.

Form_Base has a button to open the Form_Child form. Form_Base also
has button that displays a message, "From base".
The child form overrides this method and shows "From child".

It is this "From child" message that is displayed twice for some
reason.
Here's some code:

'Form_Base
-------------------------------------------------------------
Public Class Form_Base

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
OnButtonClick()
End Sub

Protected Overridable Sub OnButtonClick()
MsgBox("From base")
End Sub

Protected Overridable Sub btn_OpenChild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btn_OpenChild.Click
Form_Child.Show()
End Sub
End Class
-------------------------------------------------------------

'Form_Child
-------------------------------------------------------------
Public Class Form_Child
Inherits Form_Base

Protected Overrides Sub OnButtonClick()
MsgBox("From child")
End Sub

Protected Overrides Sub btn_OpenChild_Click(ByVal sender As
Object, ByVal e As System.EventArgs)
Me.Close()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
OnButtonClick()
End Sub
End Class
-------------------------------------------------------------

Do you see anything that is wrong here, and could result in the double
event?

Thanks.

- Christian
 
Ok,

I made a simple test with two forms. Form_Base and Form_Child.

Form_Base has a button to open the Form_Child form. Form_Base also
has button that displays a message, "From base".
The child form overrides this method and shows "From child".

It is this "From child" message that is displayed twice for some
reason.
Here's some code:

'Form_Base
-------------------------------------------------------------
Public Class Form_Base

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
OnButtonClick()
End Sub

Protected Overridable Sub OnButtonClick()
MsgBox("From base")
End Sub

Protected Overridable Sub btn_OpenChild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btn_OpenChild.Click
Form_Child.Show()
End Sub
End Class
-------------------------------------------------------------

'Form_Child
-------------------------------------------------------------
Public Class Form_Child
Inherits Form_Base

Protected Overrides Sub OnButtonClick()
MsgBox("From child")
End Sub

Protected Overrides Sub btn_OpenChild_Click(ByVal sender As
Object, ByVal e As System.EventArgs)
Me.Close()
End Sub

---- Remvoe this -------------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
OnButtonClick()
End Sub ------------------------------------------------------------------------

End Class

Remove the Button1_Click sub in the derived class. That is causing the
handler to be added twice. All you need to do is override the
OnButtonClick() method.
 
---- Remvoe this -------------------------------------------------------> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e


Remove the Button1_Click sub in the derived class. That is causing the
handler to be added twice. All you need to do is override the
OnButtonClick() method.

Yes!

That did the trick.
Now I know how to do similar things in the future. :)

Thank you so much.

- Christian.
 
On 6 maalis, 22:34, Tom Shelton
On 2008-03-06, (e-mail address removed) <[email protected]> wrote:
On 6 maalis, 21:58, Tom Shelton
On 2008-03-06, (e-mail address removed) <[email protected]> wrote:
I have this form Form_Report_Vacations which is inherited from
Form_Report_Shifts.
Form_Report_Shifts contains an event handler for a button named
cmd_Show.
In my inherited form i want to specify different behaviour for the
click event of the button.
I dont want the code that lies in the base class to execute at all.
I have done this in the base class event handler:
' Exit sub if not in base class.
If CType(sender, Control).FindForm.Name <> "Form_Report_Shifts" Then
Exit Sub
End If
' More code
...
...
End Sub
---------------------------------------------------------------------------------------------------------------
This certainly works but it looks a little bit ugly in my opinion.
Is there a better way than this to prevent execution of code in the
event handler of the base class?
- Christian
You need to mark your event handler method as protected overridable.
Then in the class that inherits your base page you override that
method and remove the MyBase.cmd_Show_Click() call and replace it with
your own code.
Let me know if you need a sample.

Seth Rowe [MVP]

Thanks for your answer.
I tried the thing you suggested and it worked. The code in the base
class event handler was not called.
The event handler in the inherited class, however, seemed to be firing
twice.
This was solved by removing the handler implicitly in the
Form_Child.Designer.vb file
and then adding it again.
I don't know if this is something you always have to do
as to prevent the event handler in the inherited class to be fired
twice.

- Christian

I think I would organize the code slightly differently. In the base
form, I would do something more like:
Public Class BaseForm
Inherits System.Windows.Forms.Form

Private Sub cmd_Show_Click (ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmd_Show.Click
OnCmdShowClick ()
Protected Overridable Sub OnCmdShowClick ()
' Default base implementation
End Sub
End Sub
Public Class Derived
Inherits BaseForm
Protected Overrides Sub OnCmdShowClick()
' derived class implementation
End Sub
End Class
I think this will solve your problem of the double event.
I tried this approach, thus removing the call to RemoveHandler, but
the event is still fireing twice.
Hmmm... I don't get that behavior here. Are you sure you tried the
above method (or similoar)? Can you post a more complete listing of the
derived forms code?
Form_Base has a button to open the Form_Child form. Form_Base also
has button that displays a message, "From base".
The child form overrides this method and shows "From child".
It is this "From child" message that is displayed twice for some
reason.
Here's some code:
'Form_Base
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
OnButtonClick()
End Sub
Protected Overridable Sub OnButtonClick()
MsgBox("From base")
End Sub
Protected Overridable Sub btn_OpenChild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btn_OpenChild.Click
Form_Child.Show()
End Sub
End Class
-------------------------------------------------------------
'Form_Child
Protected Overrides Sub OnButtonClick()
MsgBox("From child")
End Sub
Protected Overrides Sub btn_OpenChild_Click(ByVal sender As
Object, ByVal e As System.EventArgs)
Me.Close()
End Sub

---- Remvoe this -------------------------------------------------------> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
OnButtonClick()
End Sub
Do you see anything that is wrong here, and could result in the double
event?

Remove the Button1_Click sub in the derived class. That is causing the
handler to be added twice. All you need to do is override the
OnButtonClick() method.

Yes!

That did the trick.
Now I know how to do similar things in the future. :)

Thank you so much.

- Christian.

No problem....
 
Back
Top