Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

Possible to ignore base class event handler?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cride - 06 Mar 2008 11:34 GMT
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
rowe_newsgroups - 06 Mar 2008 13:58 GMT
> Hello,
>
[quoted text clipped - 30 lines]
>
> - 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]
cride83@gmail.com - 06 Mar 2008 18:35 GMT
> > Hello,
>
[quoted text clipped - 41 lines]
>
> 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
Tom Shelton - 06 Mar 2008 19:58 GMT
>> > Hello,
>>
[quoted text clipped - 61 lines]
>
> - 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.

Signature

Tom Shelton

cride83@gmail.com - 06 Mar 2008 20:24 GMT
On 6 maalis, 21:58, Tom Shelton
<tom_shel...@YOUKNOWTHEDRILLcomcast.net> wrote:

> >> > Hello,
>
[quoted text clipped - 96 lines]
> --
> Tom Shelton

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
------------------------------------------------------------------------
' Form_Report_Vacations.Designer.vb ( The derived form )
Private Sub InitializeComponent()
       RemoveHandler cmd_Show.Click, AddressOf cmd_Show_Click
End Sub
------------------------------------------------------------------------

Maybe im still missing something.

- Christian
Tom Shelton - 06 Mar 2008 20:34 GMT
> On 6 maalis, 21:58, Tom Shelton
><tom_shel...@YOUKNOWTHEDRILLcomcast.net> wrote:
[quoted text clipped - 102 lines]
> 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?

Signature

Tom Shelton

cride83@gmail.com - 06 Mar 2008 20:42 GMT
On 6 maalis, 22:34, Tom Shelton
<tom_shel...@YOUKNOWTHEDRILLcomcast.net> wrote:

> > On 6 maalis, 21:58, Tom Shelton
> ><tom_shel...@YOUKNOWTHEDRILLcomcast.net> wrote:
[quoted text clipped - 109 lines]
> --
> Tom Shelton

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
Tom Shelton - 06 Mar 2008 20:57 GMT
> On 6 maalis, 22:34, Tom Shelton
><tom_shel...@YOUKNOWTHEDRILLcomcast.net> wrote:
[quoted text clipped - 159 lines]
>         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
> -------------------------------------------------------------
>
> 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.

Signature

Tom Shelton

cride83@gmail.com - 06 Mar 2008 21:05 GMT
On 6 maalis, 22:57, Tom Shelton
<tom_shel...@YOUKNOWTHEDRILLcomcast.net> wrote:

> > On 6 maalis, 22:34, Tom Shelton
> ><tom_shel...@YOUKNOWTHEDRILLcomcast.net> wrote:
[quoted text clipped - 179 lines]
> --
> Tom Shelton

Yes!

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

Thank you so much.

- Christian.
Tom Shelton - 06 Mar 2008 21:10 GMT
> On 6 maalis, 22:57, Tom Shelton
><tom_shel...@YOUKNOWTHEDRILLcomcast.net> wrote:
[quoted text clipped - 191 lines]
>
> - Christian.

No problem....

Signature

Tom Shelton


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.