Inheritance and User-Defined Controls - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Inheritance and User-Defined Controls

Description:

The inherited class is called a subclass, a derived class, or a ... Inherit a Class in a User-Defined Assembly. Project/Add Reference. Imports the assembly ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 20
Provided by: cob
Learn more at: https://faculty.sfsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Inheritance and User-Defined Controls


1
Inheritance and User-Defined Controls
2
Inheritance
  • The process in which a new class can be based on
    an existing class, and will inherit that classs
    interface and behaviors. The original class is
    known as the base class, super class, or parent
    class. The inherited class is called a subclass,
    a derived class, or a child class.
  • Inherited classes should always have an is a
    relationship with the base class.

3
Inheritance Example
Public Class Emp Public Eid As String
Public Ename As String Public salary As
Double Public Function tax() As Double
tax salary 0.1 End Function End
Class Public Class secretary Inherits Emp
Public WordsPerMinute As Integer End Class
4
Overriding
  • When a property or method in the base class is
    not adequate for a derived class, we can override
    the base class property or method by writing one
    with the same name in the derived class.
  • The property or method in the base class must be
    declared with the Overridable keyword.
  • The overridden property or method must be
    declared with the Overrides keyword.
  • Note Keywords Overridable and Overrides apply
    only to property procedure (not properties
    declared by public variables) or method.

5
Overriding a Method
Public Class Emp Public Eid As String
Public Ename As String Public salary As
Double Public Overridable Function tax() As
Double tax salary 0.1 End
Function End Class Public Class secretary
Inherits Emp Public WordsPerMinute As
Integer Public Overrides Function tax() As
Double If salary gt 3000 Then
tax salary 0.1 Else tax
salary 0.05 End If End Function End
Class
6
Inherit a Class in a User-Defined Assembly
  • Project/Add Reference
  • Imports the assembly
  • Creates a new class to that inherits a class in
    the assembly.
  • Demo MyAssembly/Customer class

7
Creating Custom Controls
  • A customer control is a control that is designed
    by a programmer for a specific purpose. It is
    derived from the System.Windows.Forms.UserControl
    class.
  • Object Browser
  • Two ways to create a control
  • Windows Control Library Project
  • Controls can be used in multiple projects.
  • Add a new UserControl to an existing project.
  • Only in current project

8
Custom Control Example
  • A form that displays time in the four time zones.
  • Demo TestTimeZone/TimeZoneControlLibrary

9
Creating TimeZone Control
  • This control displays time in each time zone. It
    exposes a Zone property and a ShowTime method.
  • New Project/Windows Control Library
  • Design controls appearance and add any
    functionality you want.
  • Build the DLL
  • The DLL is saved in projects Bin folder.
  • Create a Windows project to test the control.
  • Right Click Windows tab of the Tool Box and
    choose Customize ToolBox
  • Click .Net Framework component
  • Click Browse to select the DLL

10
TimeZone Control Code
Public Class TimeZone Inherits
System.Windows.Forms.UserControl Enum tzone
Eastern 1 Central
Mountain Pacific End Enum Private
sysTime As Date Private tmZone As tzone
Public Property Zone() As tzone Get
Zone tmzone End Get
Set(ByVal Value As tzone) tmzone
Value End Set End Property
11
Private Sub TimeZone_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load tmZone
tzone.Eastern Use controls load event to assign
initial value to controls property End Sub
Public Sub ShowTime() sysTime Now
If tmZone tzone.Eastern Then
lblE.Text sysTime.ToLongTimeString
lblC.Text DateAdd(DateInterval.Hour, -1,
sysTime).ToLongTimeString lblM.Text
DateAdd(DateInterval.Hour, -2, sysTime).ToLongTime
String lblP.Text DateAdd(DateInterva
l.Hour, -3, sysTime).ToLongTimeString
ElseIf tmZone tzone.Central Then
lblC.Text sysTime.ToLongTimeString
lblE.Text DateAdd(DateInterval.Hour, 1,
sysTime).ToLongTimeString lblM.Text
DateAdd(DateInterval.Hour, -1, sysTime).ToLongTime
String lblP.Text DateAdd(DateInterva
l.Hour, -2, sysTime).ToLongTimeString
ElseIf tmZone tzone.Mountain Then
lblM.Text sysTime.ToLongTimeString
lblE.Text DateAdd(DateInterval.Hour, 2,
sysTime).ToLongTimeString lblC.Text
DateAdd(DateInterval.Hour, 1, sysTime).ToLongTimeS
tring lblP.Text DateAdd(DateInterval
.Hour, -1, sysTime).ToLongTimeString
Else lblP.Text sysTime.ToLongTimeStr
ing lblE.Text DateAdd(DateInterval.H
our, 3, sysTime).ToLongTimeString
lblC.Text DateAdd(DateInterval.Hour, 2,
sysTime).ToLongTimeString lblM.Text
DateAdd(DateInterval.Hour, 1, sysTime).ToLongTimeS
tring End If End Sub End Class
12
Code Using TimeZone Control
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button1.Click TimeZone1.Zone
TimeZoneControlLibrary.TimeZone.tzone.Pacific
TimeZone1.ShowTime() End Sub
13
Class Events
  • Events that are triggered in code in the class
    and can be detected by host program.
  • To declare a event
  • Public Event EventName(Argument list)
  • Ex. Public Event InvalidCode(ByVal message As
    String)
  • To raise an event
  • RaiseEvent EventName(Argument list)

14
Public Class Emp Public Event
InvalidCode(ByVal message As String) Public
Eid As String Public Ename As String
Public salary As Double Private hiddenJobCode
As Long Public Property JobCode()
Set(ByVal Value) If Value lt 1 Or
Value gt 4 Then RaiseEvent
InvalidCode("Invalide JobCode") Else
hiddenJobCode Value
End If End Set Get
JobCode hiddenJobCode End Get End
Property End Class
15
Handling Events Using WithEvents and Handles
  • Declare the object variable using the WithEvents
    keyword
  • Dim WithEvents myemp As New Emp()
  • Event handler
  • Sub HandlerName(Argumentlist) Handles event

16
Dim WithEvents myemp As New Emp() Private Sub
InvalidCodeHandler(ByVal msg As String) Handles
myemp.InvalidCode MessageBox.Show(msg)
End Sub
17
Create an Inherited User Control
  • Create a user control from an existing control
    such as TextBox, Label, etc.
  • Example Create a control, called ValidDate, that
    looks exactly like a textbox, but it will
    validate the entry for a valid date.
  • Inherits from System.Windows.Forms.TextBox
  • Properties MaximumDate, MinimumDate with default
    value
  • Event InvalidDate event

18
ValidDate Control Code
Public Class ValidDate Inherits
System.Windows.Forms.TextBox Public Event
InvalidDate(ByVal message As String) Private
maxDate As Date Now.Date.AddYears(1)
Private minDate As Date Now.Date Public
Property MaximumDate() As Date Get
MaximumDate maxDate End Get
Set(ByVal Value As Date) maxDate
Value End Set End Property Public
Property MinimumDate() As Date Get
MinimumDate minDate End Get
Set(ByVal Value As Date) minDate
Value End Set End Property
19
Private Sub ValidDate_Validating(ByVal sender As
Object, ByVal e As System.ComponentModel.CancelEve
ntArgs) Handles MyBase.Validating If Not
IsDate(Me.Text) Then Me.SelectAll()
e.Cancel True RaiseEvent
InvalidDate("Date not valid") Else
Dim enteredDate As Date CDate(Me.Text)
If enteredDate lt minDate Or enteredDate gt
maxDate Then RaiseEvent
InvalidDate("Date out of range")
Me.SelectAll() e.Cancel True
End If End If End Sub End
Class
Write a Comment
User Comments (0)
About PowerShow.com