[ Pobierz całość w formacie PDF ]
.Listing 11.16 is the function that sorts Person objectsaccording to the Age field.Listing 11.16: A Custom ComparerClass PersonAgeComparer : Implements IComparerPublic Function Compare(ByVal o1 As Object, ByVal o2 As Object) As Integer _Implements IComparer.CompareDim person1, person2 As PersonTryperson1 = CType(o1, Person)person2 = CType(o2, Person)Catch compareException As system.ExceptionThrow (compareException)Exit FunctionEnd TryIf person1.BDate person2.BDate ThenReturn 1ElseReturn 0End IfEnd IfEnd FunctionEnd ClassThe code could have been considerably simpler, but I ll explain momentarily why the Try state-ment is necessary.The comparison takes place in the If statement.If the first person s birth date isnumerically smaller than the second person s, the function returns the value 1.If the first person sbirth date is numerically smaller than the second person s, the function returns 1.Finally, if the twovalues are equal, the function returns 0.www.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c11.qxd 11/11/01 4:18 PM Page 516516 Chapter 11 STORING DATA IN COLLECTIONSThe code is straightforward, so why the error-trapping code? Before we perform any of the neces-sary operations, we convert the two objects into Person objects.It s not unthinkable that the collec-tion with the objects you want to sort contains objects of different types.If that s the case, the CType()function won t be able to convert the corresponding argument to the Person type, and the compari-son will fail.The same exception that would be thrown in the function s code is raised again fromwithin the error handler, and it s passed back to the calling code.The Person objects can be sorted in many different ways.You may wish to sort them by ID,name, and so on.To accommodate multiple sorts, you must implement several classes, each one witha different Compare function.Listing 11.17 shows two classes that implement two different Com-pare functions for the Person class.The PersonNameComparer class compares the names, while thePersonAgeComparer class compares the ages.Listing 11.17: A Class with Two Custom ComparersClass PersonNameComparer : Implements IComparerPublic Function Compare(ByVal o1 As Object, ByVal o2 As Object) As Integer _Implements IComparer.CompareDim person1, person2 As PersonTryperson1 = CType(o1, Person)person2 = CType(o2, Person)Catch compareException As system.ExceptionThrow (compareException)Exit FunctionEnd TryIf person1.Name person2.Name ThenReturn 1ElseReturn 0End IfEnd IfEnd FunctionEnd ClassClass PersonAgeComparer : Implements IComparerPublic Function Compare(ByVal o1 As Object, ByVal o2 As Object) As Integer _Implements IComparer.CompareDim person1, person2 As PersonTryperson1 = CType(o1, Person)person2 = CType(o2, Person)Catch compareException As system.ExceptionThrow (compareException)Exit FunctionEnd Trywww.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c11.qxd 11/11/01 4:18 PM Page 517THE IENUMERATOR AND ICOMPARER INTERFACES 517If person1.BDate > person2.BDate ThenReturn -1ElseIf person1.BDate 0 ThenMe.Editor.Copy()End IfEnd SubPrivate Sub EditPaste_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles EditPaste.ClickIf Clipboard.GetDataObject.GetData(DataFormats.Text) ThenMe.Editor.Paste()ElseMsgBox( no text to paste )End IfEnd SubPrivate Sub EditCut_Click(ByVal sender As Object, _ByVal e As System.EventArgs) Handles EditCut.ClickIf Me.Editor.SelectedText.Length > 0 ThenClipboard.SetDataObject(Me.Editor.SelectedText)Me.Editor.Cut()End IfEnd Subwww.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c19.qxd 11/11/01 4:21 PM Page 856856 Chapter 19 THE MULTIPLE DOCUMENT INTERFACEThe Find CommandThis is the most interesting part of the application.The Find command must display a Find &Replace dialog box, shown in Figure 19.10.This dialog box must remain on top of the application smain form, even when it doesn t have the focus.Design a form like the one shown in Figure 19.10,and set its TopMost property to True.Figure 19.10The Find & Replacedialog box of theMDIPad applicationTo invoke the Find & Replace dialog box from within a child form s code, you must create aninstance of the dialog box and then call its Show method.Insert the following declaration in thechild form s code window, outside any procedure s definition:Dim extForm As Form = New FindForm()Then add the code in Listing 19.11 to the Find command s Click event handler
[ Pobierz całość w formacie PDF ]