I want to produce a search that lets me have multiple search results but it only returns one.
How do I fix this?
My code is attached below, thanks for the help!
Public Class Form1
Dim arrayAdoptions As Array
'********************************************************************
'* Function name: Swap
'* Parameters: A (String), B (String)
'* Returns: Nil
'* Purpose: Swaps the memory location of items A and B
'*********************************************************************
Private Sub Swap(ByRef A As String, ByRef B As String)
Dim T As String
T = A
A = B
B = T
End Sub
'********************************************************************
'* Function name: QuickSort
'* Parameters: searchField (string), arraySort (string array)
'* First (integer), Last (integer)
'* Returns: Nil
'* Purpose: Sorts contents of 2D array arraySort acording to searchField
'*********************************************************************
Private Sub QuickSort(ByVal searchField As String, ByVal arraySort(,) As String, ByVal First As Integer, ByVal Last As Integer)
Dim intCount As Integer = 0
Dim intIndex = 0
Dim intLow As Integer, intHigh As Integer
Dim strMidValue As String
Select Case searchField
Case "Name"
intIndex = 0
Case "Breed"
intIndex = 1
Case "Age"
intIndex = 2
Case "Desexed"
intIndex = 3
Case "Size"
intIndex = 4
Case "Nature"
intIndex = 5
Case "Type"
intIndex = 6
Case "Trained"
intIndex = 7
Case "Hair Length"
intIndex = 8
End Select
intLow = First
intHigh = Last
strMidValue = arraySort((First + Last) \ 2, intIndex).ToLower
If intIndex <> 8 Then
Do
While arraySort(intLow, intIndex).ToLower < strMidValue
intLow = intLow + 1
End While
While arraySort(intHigh, intIndex).ToLower > strMidValue
intHigh = intHigh - 1
End While
If intLow <= intHigh Then
While intCount < 8
Swap(arraySort(intLow, intCount), arraySort(intHigh, intCount))
intCount = intCount + 1
End While
intLow = intLow + 1
intHigh = intHigh - 1
End If
Loop While intLow <= intHigh
If First < intHigh Then QuickSort(searchField, arraySort, First, intHigh)
If intLow < Last Then QuickSort(searchField, arraySort, intLow, Last)
End If
End Sub
'********************************************************************
'* Function name: BinarySearch
'* Parameters: searchField (string), arraySort (string array)
'* strSearch (string)
'* Returns: intFound (integer) index location of found item, returns
'* -1 if nothing found
'* Purpose: Searches 2D array for any items matching strSearch
'*********************************************************************
Private Function binarySearch(ByVal searchField As String, ByVal searchArray(,) As String, ByVal strSearch As String)
Dim intIndex = 0
Dim intLower As Integer = 0
Dim intUpper As Integer = (searchArray.Length / 9) - 1
Dim intMiddle As Integer
Dim intFound As Integer = -1
Select Case searchField
Case "Name"
intIndex = 0
Case "Breed"
intIndex = 1
Case "Age"
intIndex = 2
Case "Desexed"
intIndex = 3
Case "Size"
intIndex = 4
Case "Nature"
intIndex = 5
Case "Type"
intIndex = 6
Case "Trained"
intIndex = 7
Case "Hair Length"
intIndex = 8
End Select
If intIndex <> 9 Then
While intLower <= intUpper And intFound = -1
intMiddle = (intLower + intUpper) / 2
If searchArray(intMiddle, intIndex) < strSearch Then
intFound = intMiddle
ElseIf searchArray(intMiddle, intIndex) > strSearch Then
intUpper = intMiddle - 1
Else
intLower = intMiddle + 1
End If
End While
End If
Return intFound
End Function
' Private Function binaryAge(ByVal searchField As String, ByVal searchArray(,) As String, ByVal strSearch As String)
'Dim intLower As Integer = 0
'Dim intUpper As Integer = (searchArray.Length / 9) - 1
'Dim intMiddle As Integer
'Dim intFound As Integer = -1
'
' If cmbAge.Text = ">" Then 'If the cmbAge is equal to greate than'
' While intLower <= intUpper And intFound = -1
' intMiddle = (intLower + intUpper) / 2
' If searchArray(intMiddle, 2) > strSearch Then
' intFound = intMiddle
' ElseIf searchArray(intMiddle, 2) >= strSearch Then
' intUpper = intMiddle - 1
' Else
' intLower = intMiddle + 1
' End If
' End While
'End If
'
' If cmbAge.Text = "<" Then
' While intLower <= intUpper And intFound = -1
' intMiddle = (intLower + intUpper) / 2
' If searchArray(intMiddle, 2) < strSearch Then
' intFound = intMiddle
' ElseIf searchArray(intMiddle, 2) <= strSearch Then
' intUpper = intMiddle - 1
' Else
' intLower = intMiddle + 1
' End If
' End While
' End If
'
' Return intFound
'
' End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
arrayAdoptions = LoadTextFile("availableadoptions.txt")
DisplayData(arrayAdoptions)
End Sub
'********************************************************************
'* Function name: LoadTextFile
'* Parameters: FileName(string)
'* Returns: array (array of strings) array containing data from text file
'* Purpose: Loads contents of text file in Filename into array
'*********************************************************************
Private Function LoadTextFile(ByVal Filename As String)
Dim intCount As Integer = 0
Dim intCount2 As Integer = 0
Dim objRead As New System.IO.StreamReader(Filename)
While objRead.Peek() <> -1
intCount = intCount + 1
objRead.ReadLine()
End While
Dim array(intCount / 9 - 1, 9 - 1) As String
objRead.Close()
Dim objRead2 As New System.IO.StreamReader(Filename)
intCount = 0
While objRead2.Peek() <> -1
intCount2 = 0
While intCount2 < 9
array(intCount, intCount2) = objRead2.ReadLine()
intCount2 = intCount2 + 1
End While
intCount = intCount + 1
End While
objRead2.Close()
Return array
End Function
'********************************************************************
'* Function name: DisplayData
'* Parameters: arrayDisplay(array of strings)
'* Returns: Nil
'* Purpose: Displays contents of arrayDisplay to the ListView object
'* on form.
'*********************************************************************
Private Sub DisplayData(ByVal arrayDisplay(,))
Dim row(9) As String
Dim intCount As Integer = 0
Dim intCount2 As Integer = 0
Dim lvItem As New ListViewItem
While intCount < (arrayDisplay.Length - 1) / 9
intCount2 = 0
While intCount2 < 9
row(intCount2) = arrayDisplay(intCount, intCount2)
intCount2 = intCount2 + 1
End While
lvPets.Items.Add(New ListViewItem(row))
intCount = intCount + 1
End While
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim intFound As Integer = -1
Dim intCount As Integer = 0
If cmbSearch.Text = "" Then
MsgBox("Please select a search field")
Exit Sub
End If
QuickSort(cmbSearch.Text, arrayAdoptions, 0, (arrayAdoptions.Length / 9) - 1)
intFound = binarySearch(cmbSearch.Text, arrayAdoptions, txtSearch.Text)
If intFound = -1 Then
MsgBox("No search results matching criteria")
Exit Sub
End If
Dim arraySearchResults(0, 9)
lvPets.Items.Clear()
While intCount < 9
arraySearchResults(0, intCount) = arrayAdoptions(intFound, intCount)
intCount = intCount + 1
End While
DisplayData(arraySearchResults)
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtSearch.Text = ""
cmbAge.Text = ""
lvPets.Items.Clear()
DisplayData(arrayAdoptions)
End Sub
End Class
*Posted wrong code*
This post has been edited by Fiendly: 14 August 2011 - 05:00 PM

New Topic/Question
Reply



MultiQuote






|