Implementing Find Functionality


Implementing the find functionality is a little different because your find functionality should only work on the list forms, not the edit forms. To start this, let's add a new interface to the UIInterfaces code module. Create the following interface called IFind:

 Public Interface IFind      Sub Find()      Sub FindNext() End Interface 

Next, add this Implements line to the frmListBase class:

 Implements IFind 

Now add the Find method to the frmListBase class:

 Public Sub Find() Implements IFind.Find End Sub 

Note

Again, if you are using VS .NET version 1.1, this method will have already been added for you as soon as you enter the Implements line.

To make life easy, you can move all of the code from the btnFind_Click method to the Find method and then call the Find method from the btnFind_Click event, like this:

 Private Sub btnFind_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnFind.Click    Find() End Sub 

Now, add the FindNext routine as shown in Listing 6-14. The code first checks to see if anything is selected. If there is not anything selected, you cannot possibly perform a find next, so you just exit the routine. If you do have an item selected, you get the index of the selected item and start looking for the next available match. If you find it, we select it and make it visible.

Listing 6-14: The FindNext Method

start example
 Public Sub FindNext() Implements IFind.FindNext      Dim i, j, k As Integer      Dim lst As ListViewItem      If lvwList.SelectedItems.Count = 0 Then Exit Sub      Try           i = cboColumn.SelectedIndex           k = lvwList.SelectedItems(0).Index + 1           For j = k To lvwList.Items.Count - 1                lst = lvwList.Items(j)                If _ lst.SubItems(i).Text.ToUpper.StartsWith(txtSearch.Text.ToUpper) Then                     lst.Selected = True                     lst.EnsureVisible()                     Exit For                End If           Next      Catch exc As Exception           LogException(exc)      End Try End Sub 
end example

You can choose to extend these routines by adding message boxes or status bar messages when an item is not found. You can also alter these to search through all of the columns without the user having to pick a column. But these are basic, solid routines for searching through a listview.

Next, go to the frmMain form and add the method in Listing 6-15, which works exactly like the CutCopyPaste method created earlier. You are going to cheat a little because your application is the only one calling the Find and FindNext methods. Your routine accepts a value of 1 or anything else to determine whether to call the Find or FindNext methods. You could create an enumeration, but you do not need it for such a simple routine being called in only one place.

Listing 6-15: Implementing the Find/FindNext Method in the MDI Form

start example
 Private Sub Find(ByVal intType As Integer)      Dim objFind As IFind      Dim t As System.Type      If ActiveMdiChild Is Nothing Then Exit Sub      Try           t = ActiveMdiChild.GetType           If Not t.GetInterface("IFind") Is Nothing Then                objFind = CType(Me.ActiveMdiChild, IFind)                If intType = 1 Then                     objFind.Find()                Else                     objFind.FindNext()                End If           End If      Catch exc As Exception           LogException(exc)      End Try End Sub 
end example

This method checks to see if the IFind interface is implemented by the child form. If it is, it calls the Find or FindNext method on the interface. Finally, edit the Select Case statement in the MainMenu_Click event by adding the following Case statement:

 Case "&Find..."      Find(1) Case "Find &Next"      Find(0) 

This finishes implementing the Select All and Find functionality. As you can see, extending either of these interfaces is extremely simple and requires little work. It is little things like these that impress users (of course, a working application is a pretty good trick also).




Building Client/Server Applications with VB. NET(c) An Example-Driven Approach
Building Client/Server Applications Under VB .NET: An Example-Driven Approach
ISBN: 1590590708
EAN: 2147483647
Year: 2005
Pages: 148
Authors: Jeff Levinson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net