190 likes | 268 Vues
Inheritance and User-Defined Controls. Inheritance.
E N D
Inheritance • The process in which a new class can be based on an existing class, and will inherit that class’s 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.
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
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.
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 > 3000 Then tax = salary * 0.1 Else tax = salary * 0.05 End If End Function End Class
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
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
Custom Control Example • A form that displays time in the four time zones. • Demo: TestTimeZone/TimeZoneControlLibrary
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 control’s appearance and add any functionality you want. • Build the DLL: • The DLL is saved in project’s 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
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
Private Sub TimeZone_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load tmZone = tzone.Eastern ‘Use control’s load event to assign initial value to control’s 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).ToLongTimeString lblP.Text = DateAdd(DateInterval.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).ToLongTimeString lblP.Text = DateAdd(DateInterval.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).ToLongTimeString lblP.Text = DateAdd(DateInterval.Hour, -1, sysTime).ToLongTimeString Else lblP.Text = sysTime.ToLongTimeString lblE.Text = DateAdd(DateInterval.Hour, 3, sysTime).ToLongTimeString lblC.Text = DateAdd(DateInterval.Hour, 2, sysTime).ToLongTimeString lblM.Text = DateAdd(DateInterval.Hour, 1, sysTime).ToLongTimeString End If End Sub End Class
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
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)
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 < 1 Or Value > 4 Then RaiseEvent InvalidCode("Invalide JobCode") Else hiddenJobCode = Value End If End Set Get JobCode = hiddenJobCode End Get End Property End Class
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
Dim WithEvents myemp As New Emp() Private Sub InvalidCodeHandler(ByVal msg As String) Handles myemp.InvalidCode MessageBox.Show(msg) End Sub
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
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
Private Sub ValidDate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) 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 < minDate Or enteredDate > maxDate Then RaiseEvent InvalidDate("Date out of range") Me.SelectAll() e.Cancel = True End If End If End Sub End Class