Results 1 to 13 of 13

Thread: Class to Component?

  1. #1

    Thread Starter
    Hyperactive Member Skatebone's Avatar
    Join Date
    Jan 2009
    Location
    Malta
    Posts
    279

    Class to Component?

    Heyy guys

    I have created a class and want to distribute it without giving off my code/create it as an application.

    I am aware that components are nearly no longer used and their disadvantages.

    I dont need to draw anything on the ui. (I know basically components are used for this aim but I just need to try this :] )

    My question is:

    My class uses a webbrowser because webclient and web request don't work for me. When I start vs 2010 and select new project - Class Library and include the namespace System.Windows.Forms it doesnt find it. So first of all should I select class library to create a component and how could I include the webbrowser object?

    Thanks in advance ( Ps I know the webbrowser is not ideal but please I still wanna use it )
    Life runs on code

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: Class to Component?

    You mean the following does not work . What is the error ?
    vb Code:
    1. Imports System.Windows.Forms
    2.  
    3. Public Class Class1
    4.     Dim wb As WebBrowser
    5.  
    6. End Class
    thanks
    amrita

  3. #3

    Thread Starter
    Hyperactive Member Skatebone's Avatar
    Join Date
    Jan 2009
    Location
    Malta
    Posts
    279

    Re: Class to Component?

    For: Imports System.Windows.Forms

    Nope It doesnt.

    That Namespace doesnt exist. (highlighted with green line giving no help)
    Life runs on code

  4. #4

  5. #5
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: Class to Component?

    Quote Originally Posted by Skatebone View Post
    For: Imports System.Windows.Forms

    Nope It doesnt.

    That Namespace doesnt exist. (highlighted with green line giving no help)
    Add reference to System.Windows.Forms
    thanks
    amrita

  6. #6

    Thread Starter
    Hyperactive Member Skatebone's Avatar
    Join Date
    Jan 2009
    Location
    Malta
    Posts
    279

    Re: Class to Component?

    Thanks and how should I compile it as a component?

    Thanks alot : )
    Life runs on code

  7. #7
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: Class to Component?

    Use
    Code:
     implements IComponent
    and add code to the methods.
    thanks
    amrita

  8. #8

  9. #9

    Thread Starter
    Hyperactive Member Skatebone's Avatar
    Join Date
    Jan 2009
    Location
    Malta
    Posts
    279

    Re: Class to Component?

    Therefore I add:

    Code:
    Imports System.ComponentModel
    And What? I programm everything normally? How can I add properties ect to my component and test it?

    Thanks
    Life runs on code

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

    Re: Class to Component?

    A Component is no different to any other class, except that it can be designed visually. You add members to your component the same way you add members to any other class. You test your component by using it in an application, just as you use any other component.

    Generally speaking, you should create a solution with two projects: one that compiles to a DLL and contains your component and another that compiles to an EXE that you use as a test rig. Any time you make changes to the DLL, you build the solution and run the application project to test.
    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

  11. #11

    Thread Starter
    Hyperactive Member Skatebone's Avatar
    Join Date
    Jan 2009
    Location
    Malta
    Posts
    279

    Re: Class to Component?

    Quote Originally Posted by jmcilhinney View Post
    A Component is no different to any other class, except that it can be designed visually. You add members to your component the same way you add members to any other class. You test your component by using it in an application, just as you use any other component.

    Generally speaking, you should create a solution with two projects: one that compiles to a DLL and contains your component and another that compiles to an EXE that you use as a test rig. Any time you make changes to the DLL, you build the solution and run the application project to test.
    Ahh so I put my design code in here right?:

    Code:
    Interface IComponent
    
    End Interface
    This looks cool thanks alot
    Life runs on code

  12. #12
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Class to Component?

    No, not at all. That would be when you were writing an interface. If you are implementing an interface, you use
    Code:
    Public Class <name>
       Implements IComponent
    
       'members that implement IComponent members
    End Class
    But as I said, I don't see any reason for you to implement IComponent manually, as you can just inherit Component. Then you don't have to write any of the IComponent methods yourself.
    Component is the default implementation of IComponent and serves as the base class for all components in the common language runtime.

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

    Re: Class to Component?

    I'm not sure if VB Express offers the option but, in VS, you don't even have to inherit Component manually. You simply add an item to your project as you usually do, i.e. use the Project menu or right-click the project in the Solution Explorer and select Add. You then just select Component instead of Class or Windows Form or whatever.

    Also, if you intend to use WinForms functionality in a DLL then start by creating a Windows Control Library project rather than a Class Library. It already has the appropriate references and imports. Again, I'm not 100&#37; sure VB Express offers that template.
    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