Results 1 to 4 of 4

Thread: [RESOLVED] Call method on inherited UserControls

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Resolved [RESOLVED] Call method on inherited UserControls

    I have a user control that I use for a base for a number of inherited controls:
    VB Code:
    1. Public Class JobDisplayBlock
    2.     Inherits System.Windows.Forms.UserControl
    3. '
    4. '
    5. End Class

    From this class, I inherit a number of different classes, as below. All the inherited classes have a method called Clear, which I call to reset text, numbers, etc within the class:
    VB Code:
    1. Public Class StationDisplay
    2.     Inherits JobDisplayBlock
    3.  
    4.     Public Sub Clear()
    5.         'Code to zero counts and clear text, etc
    6.     End Sub
    7.  
    8. End Class

    How can I loop through all these classes and call the Clear method on each one, just by checking if the type of object is the base class?
    VB Code:
    1. Public Shared Sub ClearJobLabels(ByRef ctlColl As Control.ControlCollection)
    2.  
    3.         For Each ctl As Control In ctlColl
    4.  
    5.             If TypeOf ctl Is JobDisplayBlock Then
    6.  
    7.                 'Call Clear method on all inherited classes
    8.  
    9.             End If
    10.  
    11.             If ctl.HasChildren Then
    12.                 ClearJobLabels(ctl.Controls)
    13.             End If
    14.  
    15.         Next ctl
    16.  
    17.     End Sub

    Hope that makes sense, thanks.

    EDIT: I am using VB2003 standard.
    Last edited by Andy_P; Aug 6th, 2006 at 07:45 AM. Reason: Forgot to mention VB version!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Call method on inherited UserControls

    If JobDisplayBlock exists solely as a base for other classes then you should declare it MustInherit. You can then declare a Clear method as MustOverride and then override it in every derived class. That will allow you to call Clear on a JobDisplayBlock reference and invoke the derived class's implementation. If you will want to be able to create actual instances of the JobDisplayBlock class itself then you can't declare it MustInherit. In that case just declare a Clear method as Overridable and then override it in each derived class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: Call method on inherited UserControls

    The JobDisplayBlock class is used soley as a base class, so declaring it as MustInherit, and declaring the sub as MustOverride was the key.

    It now works as I had hoped!

    Many thanks for your help once again.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Call method on inherited UserControls

    Don't forget to mark your thread as Resolved.

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