Results 1 to 4 of 4

Thread: User Control Inheritance and Overriden procedures

Hybrid View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769

    User Control Inheritance and Overriden procedures

    Hi,

    I have a ControlBase object which I am inheriting all of my user controls from and I have overriden a method in the extended user control. This overriden method runs fine most of the time, but every now and again, the application does not run the overriden procedure. It continues excecution as though the method was never overriden.

    Here is an example

    VB Code:
    1. Public Class ControlBase
    2.     Inherits System.Windows.Forms.UserControl
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    7.         OnBeforeSave()
    8.         OnSave()
    9.         OnAfterSave()
    10.     End Sub
    11.  
    12.     Protected Overridable Sub OnSave()
    13.         '
    14.     End Sub
    15.  
    16.     Protected Overridable Sub OnBeforeSave()
    17.         '
    18.     End Sub
    19.  
    20.     Protected Overridable Sub OnAfterSave()
    21.         '
    22.     End Sub
    23.  
    24. End Class
    25.  
    26. Public Class ExtendedControl
    27.     Inherits WindowsApplication3.ControlBase
    28.  
    29. #Region " Windows Form Designer generated code "
    30.  
    31.     Protected Overrides Sub OnAfterSave()
    32.         MyBase.OnAfterSave()
    33.         MsgBox("hello world from onaftersave")
    34.     End Sub
    35.  
    36.     Protected Overrides Sub OnBeforeSave()
    37.         MyBase.OnBeforeSave()
    38.         MsgBox("hello world from onbeforesave")
    39.     End Sub
    40.  
    41.     Protected Overrides Sub OnSave()
    42.         MyBase.OnSave()
    43.         MsgBox("hello world from onsave")
    44.     End Sub
    45. End Class

    The excecution path looks like this:
    ControlBase -> btnSave_Click
    ExtendedControl -> OnBeforeSave
    ControlBase -> OnBeforeSave
    ControlBase -> OnSave
    ExtendedControl -> OnAfterSave
    ControlBase -> OnAfterSave

    I thought I had done something wrong, but it sometimes runs the overriden code and sometimes not. Has anyone else had this problem or am I just smoking something?

    Win2ksp3
    .NET 2003
    Framework V1.1.4322

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I haven't noticed any problems yet. That is wierd. Is it always the same method that does act right? If so maybe its something related to that event specifically.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    It seems to be that one procedure, but on many inherited user controls.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Maybe the parent class is calling it via its own MyBase call or something and thus skipping it. Does that one always not work? Or does that method sometimes work?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width