Вы находитесь на странице: 1из 16

Introducing User Controls and Managing

Components

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

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


Introducing User Controls and Managing
Components

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.

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


Introducing User Controls and Managing
Components

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.

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


Introducing User Controls and Managing
Components

Just a Minute…
2. Which method will you override to render graphical users
to customized controls?
3. What format string will you use to specify a date in the
year-month-date format?
4. How will you ensure that an entire control is redrawn?

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


Introducing User Controls and Managing
Components

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.

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


Introducing User Controls and Managing
Components

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.

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


Introducing User Controls and Managing
Components

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.

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


Introducing User Controls and Managing
Components

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.

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


Introducing User Controls and Managing
Components

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.

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


Introducing User Controls and Managing
Components

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.

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


Introducing User Controls and Managing
Components

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)

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


Introducing User Controls and Managing
Components

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.

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


Introducing User Controls and Managing
Components

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

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


Introducing User Controls and Managing
Components

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.

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


Introducing User Controls and Managing
Components

Just a Minute…
2. Identify the syntactical error in the following code snippet.
3. Fill in the blank:
In Visual Basic .NET, delegates are ___________ types
base on the System.Delegates class.

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


Introducing User Controls and Managing
Components
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.

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

Вам также может понравиться