Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

VB.NET Creating and Managing Components Part 1

By Exforsys | on July 6, 2005 |
VB.NET 2005

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

.

.

.

After adding the control to the ToolBox, you can also add three text boxes, two buttons and five labels and arrange them as you would see in the final output form shown below.

In the code editor add the following code to the Form1:

Click here for Sample Code

Now you can press F5 to execute the program. The output of the program is shown below:

Creating and implementing Events

In the example above validations for data entry have not been touched upon. In this section we shall see how validations can be added to the component. We shall assume a business rule that this calculator is used to calculate interest for Principal exceeding 100 for interest rate > 4 where the number of years is 2 or more.

For this purpose we shall create events. We shall also add programs to check the values and raise the events. The events that are raised will be handled in the windows application with the procedures that use the Handles keyword.

Events are declared using the keyword Event:

Event event name (Argument)

The event can be raised using the RaiseEvent keyword as shown below:

RaiseEvent event name

The modified code for the user control is given below:

Click here for Sample Code

The modified code for the windows application using this control is also shown below:

Click here for Sample Code

Now press F5 to execute the program. The following screenshots show us the way the three events are handled along with the normal termination of the program.

The error message

 

 

 

 

 

 

 

 

 

 

 

Extending a control through Visual Inheritance

The concept of Visual inheritance was introduced to facilitate the use and reuse of forms in VB.NET applications. It is Microsoft’s way of describing visual implementation of interfaces as objects. A base form is created and located in the class library project for use of multiple applications. When a form inherits from a base form, the controls on the base form appear in the derived form and cannot be modified if the modifier property of the base form has been set to Private or Friend. Therefore, if the derived form is to have the option of modifying the controls, the base form should have the control modifier properties set to Public or Protected.

The base form project will have to be compiled every time changes are made to the form. A change in one location will impact on all forms derived from the base form if the modifier property of the form is set to Private or Friend.

To derive a form object, the user has to simply add a new inherited form to his project. The inheritance picker will be launched and the user will be prompted to identify the base form. The application developer can choose the form available in the same assembly or can add another assembly and inherit a form from that assembly. In this case the developer will have all the display components of the parent form available to the newly added form.

Visual inheritance enables the programmers create user interfaces with ease. The capacity to inherit controls along with the form makes for rapid application development and generation of customized controls becomes easier.

.

.

.

Inheriting from a UserControl

1. In Visual Studio create a new Control Class project.


2. Add to the class labels and textboxes as shown in the screenshot below.


3. You can also change the background color and any other display component property.

Let us assume that this control has been created to extend support for data entry across an organization. The control is then distributed to regions having their own servers and data structure. This control is created to provide support for data-entry to an organization. It is then distributed with each region having its own server and data structure. To provide a consistent look and feel over the organization this UserControl is devised. The SQL statement, connect string and organization name are some of the properties that can be set by external code. The following lines of code are added to the class.

Click here for Sample Code

In a new windows control project add new inherited control by right-clicking the project in the solution explorer. This will open up the “Add New Item” dialog box as shown below. In this dialog box choose the option inherited user control:

This will open the inheritance picker. Click on the browse button and navigate to the bin directory of the original UserControl that you had created earlier.. Choose the dll file and click OK. The screen shot is given below:

 

 

 

 

 

 

 

 

 

 

A new class is created and the code view of this class shows Visual Studio generated code. You can add your own code to these existing codes. The sample lines of code are given below:

Click here for Sample Code

You click on the Build menu and build the solution. This control can be added to the ToolBox and you can use this in any windows forms application.

The screen shot of the data – entry screen with incorrect value for Principle after the error message is dismissed

Error Message

The screen shot of the data – entry screen with incorrect value for Interest Rate after the error message is dismissed

« « VB.NET Creating and Managing Components Part 2
Career Track: Quality Assurance (Testing) » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 391 Followers
  • Popular
  • Recent
  • Working with Legacy Code and COM Components

    July 30, 2005 - 0 Comment
  • Implementing Inheritance

    June 5, 2005 - 0 Comment
  • VB.NET Creating and Managing Components Part 2

    July 6, 2005 - 0 Comment
  • .NET Common Windows Forms Controls Part 1

    June 26, 2005 - 0 Comment
  • Data Manipulation with ADO.NET

    July 15, 2005 - 0 Comment
  • Windows Application Testing

    July 30, 2005 - 0 Comment
  • Visual Studio.NET Namespaces

    June 7, 2005 - 0 Comment
  • .NET Assemblies

    July 7, 2005 - 0 Comment
  • DomainUpDown and NumericUpDown Controls

    June 27, 2005 - 0 Comment
  • Using XML Data

    July 14, 2005 - 0 Comment
  • Microsoft .NET Creating Installation Components

    August 10, 2005 - 0 Comment
  • Shared Assembly

    August 9, 2005 - 0 Comment
  • The File Types Editor

    August 9, 2005 - 0 Comment
  • Tracing VB.NET Windows Application

    August 9, 2005 - 0 Comment
  • VB.NET Windows Application Testing

    August 9, 2005 - 0 Comment
  • The Registry Editor in Visual Studio.NET 2005

    August 4, 2005 - 0 Comment
  • Customizing Setup Project in Visual Studio.NET 2005

    August 4, 2005 - 0 Comment
  • Deploying Windows Applications In Visual Studio.NET 2005

    August 3, 2005 - 0 Comment
  • Debugging Windows Applications In Visual Studio.NET 2005

    August 3, 2005 - 0 Comment
  • Working with Legacy Code and COM Components

    July 30, 2005 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • Microsoft .NET Creating Installation Components
  • Shared Assembly
  • The File Types Editor
  • Tracing VB.NET Windows Application
  • VB.NET Windows Application Testing

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT