alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow VB.NET 2005 arrow VB.NET Creating and Managing Components Part 1
Site Search


VB.NET Creating and Managing Components Part 1
Article Index
VB.NET Creating and Managing Components Part 1
Page 2
Page 3

VB.NET Creating and Managing Components Part 1

In this tutorial you will learn about Components, Best practices in using Components, Creating Components by extending the UserControl Class, Testing the Control, Creating and implementing Events, Extending a control through Visual Inheritance and Inheriting from a UserControl.

A component is a reusable piece of code in binary form. This code can be reused by inheritance. No class is being inherited. It follows that a containment relationship is defined between the application using the component and the component that is being used. This is different from the relationship that exists between a derived class and a base class.

Components have to interact with each other. They need information about each other. This is achieved by loading the components within self-contained packages called assembly. An assembly contains information about the files on which the component depends and the location of these files. The CLR can use this information to determine the dependencies of a component. The assemblies that are required during the execution of an application are called dependencies.

A class becomes a component when it follows defined standards of interaction. These standards are provided by the IComponent interface. All components derive from Component class. The Component class in turn implements IComponent interface.

Best practices in using components

  • The name of the component to be short and meaningful.
  • The access level for constructors should be implemented as private or public based on the usage is either by the same assembly or different assembly.
  • The base class of all components is Component class. You can also implement IComponent interface to create a component.
  • The namespace structure in an assembly should be according to the internal organization of the component. It is preferable to keep all the components together in separate Namespace.
  • The component will have two type of initialization namely, type initialization and instance initialization.
  • Type initialization is achieved by using a shared constructor and this is done only once in the lifetime of the application.
  • Instance initialization is achieved by using the constructors which are not shared.
  • A component like any other class must implement as many interfaces as needed. They are capable of being called in multiple ways.(polymorphism).

Creating components by extending the UserControl Class

Objects help users control the flow of applications. Objects like buttons and ComboBoxes are controls. Controls can be defined as visual components that are used across applications. Controls that are customized by users are called UserControls. User Controls can have multiple child controls and provide the user with a single interface.

Let us now create a user control.

Open the Visual Studio IDE and select the menu File, Choose New – Project to open a new project Dialog Box

Choose Windows Control Library from the items displayed. Type the name of the project as InterestCalculator. Click on OK.

In the solution Explorer window, right-click UserControl1 and select View Code

You can also change the default name UserControl1 to any name of your choice

The class inherits from the UserControl class by default. If you need to inherit from another existing control, edit the statement Inherits System.Windows.Forms.UserControl to refer to your class.

Save the project.

Enter the following code in to the windows.

Click here for Sample Code

Testing the Control

The control is always used in a container. Therefore you need a windows form to test the control. The Steps for doing this are as follows.

Build the control by clicking Build menu.

Create a new windows application project

In the solution explorer window of the new project, right –click the Reference node. Select Add reference to open the Add reference dialog box.

Add the project with your custom control to the selected Components section in the AddReference DialogBox. To close the Add Reference dialog box.

Add the control to you toolbar

In the dialog box that you have opened select .NET Framework Component tab from the Add Reference Dialog Box.

Choose the ToolBox Item



 
< Prev   Next >
Sponsored Links
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape