16
Introducing User Controls and Managing Components ©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 1 of 16 Objectives In this lesson, you will learn to: Create customized user controls Add existing components and controls to a user control Declare and raise events in a user control Call methods of other objects by using delegates

VB .net tutorial - 14

Embed Size (px)

Citation preview

Page 1: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 1 of 16

Objectives

In this lesson, you will learn to:

Create customized user controls

Add existing components and controls to a user control

Declare and raise events in a user control

Call methods of other objects by using delegates

Page 2: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 2 of 16

User Controls

Are custom controls that can be created by using a number of existing controls.

Can contain several child controls.

UserControl Class

Helps in creating composite reusable controls with selectively exposed properties.

Can be used to combine the functionality of different controls into one unit that can be reused.

Presents a single and unified interface that contains members inherited from the control classes.

Page 3: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 3 of 16

Customized User Control

Can be created in two ways:

By inheriting an existing UserControl class and then customizing it to suit your requirements.

By inheriting from Control class to build the user controls from scratch.

Page 4: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 4 of 16

Just a Minute…

1. Which method will you override to render graphical users to customized controls?

2. What format string will you use to specify a date in the year-month-date format?

3. How will you ensure that an entire control is redrawn?

Page 5: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 5 of 16

Problem Statement 14.D.1

Diaz Telecommunications requires an application to validate the credit cards of customers. The expiry date of the credit card should also be validated. The development team has decided that the screen to accept the credit card details and the code to validate the card details should be packaged together as one unit because they are related. They have also observed that this unit can then be reused across applications. The project leader has informed the team that this application is being developed keeping in mind the customers in countries using the British format of date.

Page 6: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 6 of 16

Task List

Identify the component to be added to the user control.

Identify the controls to be added to the user control.

Add controls and components to the user control.

Write code for the user control.

Verify the execution of the user control.

Page 7: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 7 of 16

Task 1: Identify the component to be added to the user control.

Result:

A component to validate credit cards already exists. This component CreditCardValidator can be reused.

Page 8: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 8 of 16

Task 2: Identify the controls to be added to the user control.

Result:

Since the application is being developed for the customers of countries using the British format of date, the date

should be accepted in the British format. A customized control, BritshDateTimePicker, will be used to accept the expiry date of the credit cards.

Two text boxes will be added to accept the customer’s name and card number.

A button will be added to initiate the validation event.

Page 9: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 9 of 16

Task 3: Add controls and components to the user control.

You can design user controls by using the User Control Designer window.

Task 4: Write code for the user control.

Constituent controls

Are the controls placed in a user control.

Are private by default. Therefore, to expose the properties of a constituent control, you must create a property in the user control.

Task 5: Verify the execution of the user control.

Page 10: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 10 of 16

Problem Statement 14.P.1

Diaz Telecommunications wants an application for the performance appraisal of their employees. Employees will be categorized into three categories: excellent, good, and average. An employee will be categorized by productivity, which is calculated by dividing the amount of time spent in call‑related tasks with the total amount of time logged by an employee. The productivity value is then multiplied by ten and rounded off to the nearest integer value.

Create a component to determine the category of an employee. Create a user control to accept the employee name, time logged, and the amount of time spent in call‑related tasks. Add the component to the user control and test the user control.

Page 11: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 11 of 16

Events in Custom Controls

Allow you to raise events for user entries and allow the users to handle the event as per their requirements.

Are declared using the Event keyword.

Example

Event UserEvent (ByVal EventNo As Integer)

Are raised using the RaiseEvent keyword.

Example

RaiseEvent UserEvent (1)

Page 12: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 12 of 16

Events in Custom Controls (Contd.)

Are declared as Public.

Are handled by a special type of procedure called event-handlers.

Events Sender

Any object capable of raising an event is an event sender.

Page 13: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 13 of 16

Event Handlers

Special type of Sub procedures that are called when an event occurs.

Are declared using the Handles keyword.

Can handle multiple events.

Example

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, MenuItem1.Click

' some code

End Sub

Page 14: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 14 of 16

Delegates in Custom Controls

Are used to dynamically associate event handlers with events.

Are object‑oriented function pointers since they allow a function to be invoked indirectly by using a reference to the function.

Are based on the class System.Delegate.

Are created implicitly by the AddressOf operator.

Page 15: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 15 of 16

Just a Minute…

1. Identify the syntactical error in the following code snippet.

2. Fill in the blank:

In Visual Basic .NET, delegates are ___________ types base on the System.Delegates class.

Page 16: VB .net tutorial - 14

Introducing User Controls and Managing Components

©NIIT Introducing User Controls and Managing Components/Lesson 14/Slide 16 of 16

Summary

In this lesson, you learned that:

User controls are visual components.

You can create a reusable unit of controls and selectively expose their properties by inheriting from the UserControl class.

You can create a customized user control by inheriting from an existing control.

You can override the OnPaint( ) method to customize the drawing of a control.

You can declare, raise, and handle events.

Delegates are objects that you use to call the methods of other objects.