Inheritance and User-Defined Controls. Inheritance The process in which a new class can be based on an existing class, and will inherit that class’s interface.

Slides:



Advertisements
Similar presentations
Practical Programming COMP153-08S Lecture: Repetition Continued.
Advertisements

Creating Custom Controls. Overriding ToString Method Every class you create in VB.Net is derived from a built-in class named object. –Mscorlib/system/object.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Creating Custom Controls. Overriding ToString Method Every class you create in VB.Net is derived from a built-in class named object. –Mscorlib/system/object.
Creating Custom Controls ISYS 512/812. Inheritance The process in which a new class can be based on an existing class, and will inherit that class’s interface.
VB Classes - 2 ISYS 573. Creating an Array of Objects Dim emps(2) As emp Dim i As Integer For i = 0 To emps.GetUpperBound(0) emps(i) = New emp() Next.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Odds and Ends Component Tray Menu and contextmenu Splash Screen.
VB Classes - 2 ISYS 573. Creating an Array of Objects Dim emps(2) As emp Dim i As Integer For i = 0 To emps.GetUpperBound(0) emps(i) = New emp() Next.
Advanced Object-Oriented Programming Features
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Creating Custom Controls. Overriding ToString Method Every class you create in VB.Net is derived from a built-in class named object. –Mscorlib/system/object.
VB Classes BICS546. Adding a Class to a Project Project/Add Class Class will be saved in a.CLS file Steps: –Adding properties Declare Public variables.
VB.NET Classes ISYS 812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA.
CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Visual Basic 2008 Express Edition The IDE. Visual Basic 2008 Express The Start Page Recent Projects Open an existing project Create a New Project.
1 Graphical User Interfaces Part 2 Outline Multiple Document Interface (MDI) Windows Visual Inheritance User-Defined Controls.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.
1- Date TimePicker 2- Month Calendar 3- User Defined Controls.
Menus,MonthCalender, DateTimePicker, MDI,Tree View, List View,
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Menus,MonthCalender, DateTimePicker, MDI,Tree View, List View,
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Tutorial 6 The Repetition Structure
CIS 338: Classes and Modules Dr. Ralph D. Westfall May, 2011.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Objects andVB Classes ISYS 350. What Is an Object? Objects are key to understanding object-oriented technology. There are many examples of real-world.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
3.2 VB.NET Events An Event Procedure Properties and Event Procedures of the Form Tab Order of Controls Exercises.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties.
Objects andVB Classes ISYS 350. What Is an Object? Objects are key to understanding object-oriented technology. There are many examples of real-world.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
Visual Basic. The Close Method The Close method is used to close a form. To close a form use the keyword Me to refer to the form. Me.Close()
Introduction to VB programming Dr. John P. Abraham UTPA Chapters 2 & 3.
Chapter 13 Copyright 2000 All rights reserved 1 Chapter 13 Object-Oriented Programming.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Computer Science Up Down Controls, Decisions and Random Numbers.
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Visual Basic Fundamental Concepts
IS 350 Application Structure
Apply Procedures to Develop Message, Input, and Dialog Boxes
Introduction to VB programming
Object-Oriented Programming: Inheritance
للمزيد زورونا على موقعنا الإلكتروني:
Chapter 11 – Object-Oriented Programming
Visual Basic..
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Object-Oriented Programming: Inheritance
Tonga Institute of Higher Education
VB Classes ISYS 512.
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
GUI Programming in Visual Studio .NET
Presentation transcript:

Inheritance and User-Defined Controls

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 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 maxDate Then RaiseEvent InvalidDate("Date out of range") Me.SelectAll() e.Cancel = True End If End Sub End Class