Results 1 to 4 of 4

Thread: Adding a method to all forms existing forms without changing their code

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Adding a method to all forms existing forms without changing their code

    Hi was just wondering if it is possible to add a method to all forms in my project without having to do it on one form and Inherit all my other forms from that one

    Thanks
    Kris

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

    Re: Adding a method to all forms existing forms without changing their code

    In order for a type to have a method it must either declare that method itself or it must inherit it form its base class. There are no other ways.
    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
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Adding a method to all forms existing forms without changing their code

    The following seems to work:

    Code:
    Friend Module foo
    	<System.Runtime.CompilerServices.Extension> _
    	Friend Sub test(ByVal myForm As System.Windows.Forms.Form)
    		MessageBox.Show("seems to work")
    	End Sub
    End Module
    Call it without specifying the parameter - e.g., Me.test()

    You'll need to reference System.Core.dll to make this work.
    Last edited by David Anton; Jul 28th, 2009 at 08:26 PM.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

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

    Re: Adding a method to all forms existing forms without changing their code

    Quote Originally Posted by David Anton View Post
    The following seems to work:

    Code:
    Friend Module foo
    	<System.Runtime.CompilerServices.Extension> _
    	Friend Sub test(ByVal myForm As System.Windows.Forms.Form)
    		MessageBox.Show("seems to work")
    	End Sub
    End Module
    Call it without specifying the parameter - e.g., Me.test()
    I was going to suggest an extension method myself but decided not to because it has severe limitations. As you have, you would need to extend the Form class in order for it to be callable on all forms in the project. As such you would only have access the public members of the Form class within the method. You wouldn't have access to any Protected members of the Form class and you wouldn't have access to any members of each specific form. As such it's unlikely to be especially useful.
    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

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