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.

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Advertisements

Practical Programming COMP153-08S Lecture: Repetition Continued.
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.
VB.Net Loops.
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.
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
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.
Introduction to Web Application Development with.Net and Web Service ISYS 350.
ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn.
Coding ADO.NET Objects: Connection, Command, DataReader.
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.
VB.NET Classes ISYS 812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
VB.NET Classes ISYS 546. Classes A class is program structure that defines a user-defined data type that are used to create objects. An instance of a.
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.
VB.Net Loops. Loop FOR index – start TO end [STEP step] [statements] [EXIT FOR] NEXT index DO [{WHILE| UNTIL} condition] [statements] [EXIT DO] LOOP.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
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.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
1- Date TimePicker 2- Month Calendar 3- User Defined Controls.
Menus,MonthCalender, DateTimePicker, MDI,Tree View, List View,
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
© 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 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
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.
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.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
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.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
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.
VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Visual Basic Fundamental Concepts
Coding ADO.NET Objects: Connection, Command, DataReader
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Apply Procedures to Develop Message, Input, and Dialog Boxes
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Introduction to VB programming
Object-Oriented Programming: Inheritance
للمزيد زورونا على موقعنا الإلكتروني:
Visual Basic..
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Object-Oriented Programming: Inheritance
Coding ADO.NET Objects: Connection, Command, DataReader
Database Handling Class and Service
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.
Presentation transcript:

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 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.

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

Class Events Events that class can raise. Different from user events such as mouse clicks, these events are triggered in code in the class and can be detected by the host program. An event is declared in a class definition using the Event keyword. The event declaration statement declares the name of the event and types of arguments that the event has. –Public Event EventName(Argument list) To raise an event, use the RaiseEvent statement: –RaiseEvent EventName(Argument List)

Trapping Events with WithEvents To trap class events, declare a class variable with the WithEvents keyword. –Dim WithEvents emp1 As New emp() Create a event procedure to response to the event: –Every event procedure has a Handles keyword to identify the event it handles. The name of a event procedure is meaningless. Private Sub emp1_calTax() Handles emp1.CalTax

Class Event Example Public Class emp Event InvalidJobCode() Pblic Function tax(ByVal salary As Double) As Double tax = salary * 0.1 End Function Public Property JobCode() Set(ByVal Value) If Value 4 Then RaiseEvent InvalidJobCode() Else hiddenJobCode = Value End If End Set Get JobCode = hiddenJobCode End Get End Property

Handle Event Dim WithEvents emp1 As New emp() Private Sub invCode() Handles emp1.InvalidJobCode MessageBox.Show("invalide code") End Sub

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. 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

MyListBox Control Able to show three types of list. Properties: –Type: Enumeration type: Fruits, Digits, Letters –MySelectedItem Method: –CreateList Event –MySelectedEvent

Creating MyListBox Control New Project/Windows Control Library Design control’s appearance and add any functionality you want. –A control library may have many controls Build the DLL: –The DLL is saved in project’s Bin/Release folder. Create a Windows project to test the control. –Right Click Windows Forms tab of the Tool Box and select Choose Items –Click.Net Framework component –Click Browse to select the DLL

Public Class myList Inherits System.Windows.Forms.UserControl Private hiddenType As listType Public Event MySelectedEvent() Public Property type() As listType Get type = hiddenType End Get Set(ByVal Value As listType) hiddenType = Value End Set End Property Private hiddenSelectedItem As String Public ReadOnly Property MySelectedItem() As String Get selecteditem = hiddenSelectedItem End Get End Property Enum listType fruits = 0 digits letters End Enum

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged hiddenSelectedItem = ListBox1.SelectedItem RaiseEvent MySelectedEvent() End Sub Public Sub CreateList() ListBox1.Items.Clear() If type = listType.fruits Then ListBox1.Items.Add("apple") ListBox1.Items.Add("banana") ListBox1.Items.Add("orange") ElseIf type = listType.digits Then ListBox1.Items.Add("1") ListBox1.Items.Add("2") ListBox1.Items.Add("3") Else ListBox1.Items.Add("a") ListBox1.Items.Add("b") ListBox1.Items.Add("c") End If End Sub End Class

Testing Custom Controls Create and build the control. Keep the custom control project open, and go to Windows desktop to open a second Visual Studio window. In the second Visual Studio window, open a project to test the custom control.

Code Using MyListBox Control Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click MyListBox1.type = InputBox(“enter type code:”) MyListBox1.CreateList() End Sub Private Sub MyListBox1_selectedEvent() Handles MyListBox1.MySelectedEvent textbox1.text = MyListBox1.MySelectedItem End Sub

Create a GoodDateBox Textbox Create a control, called GoodDateBox, 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 Date entered must be between MinimumDate and MaximumDateNow. The fault values for Minimumdate and Maximum date are Now and and a year from now.

ValidDate Control Code Public Class GooddateBox 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 TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating If Not IsDate(TextBox1.Text) Then TextBox1.SelectAll() e.Cancel = True RaiseEvent InvalidDate("Date not valid") Else Dim enteredDate As Date = CDate(TextBox1.Text) If enteredDate maxDate Then RaiseEvent InvalidDate("Date out of range") TextBox1.SelectAll() e.Cancel = True End If End Sub

Creating a Customer Data Entry Control Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLInsert As String strSQLInsert = "Insert into Customer values ('" strSQLInsert = strSQLInsert & TextBox1.Text & "','" & TextBox2.Text & "','" strSQLInsert = strSQLInsert & TextBox3.Text & "','" & TextBox4.Text & "')" Dim objCommInsert As New OleDbCommand(strSQLInsert, objConn) objCommInsert.ExecuteNonQuery() End Sub

Web User Control