Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Creating Custom Controls ISYS 512/812

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

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

5 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

6 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

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

8 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

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

10 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

11 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

12 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

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

14 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

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

16 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

17 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

18 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

19 Web User Control


Download ppt "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."

Similar presentations


Ads by Google