Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a program in which teachers enter in students names and corresponding marks. I have completed the entering of data into a listview in a separate form, however I am having trouble creating a search form which searches through the students names entered, and displays in a message box whether the students name is found with their mark that has been entered, or "Name not found". The search is completed by entering the search item into a textbox, and then clicking a button called search, where in the listview, 0 is the item of the students name and subitem(1) is the students mark.

So far I have the following code:
VB
Dim SearchItem As ListViewItem = frmStudentRecords.ListView1.FindItemWithText(txtSearchItem.Text, False, 0, True)

        If SearchItem IsNot Nothing Then
            Me.Hide()
            frmStudentDatabase.Show()
 SearchItem.Selected = True
            MsgBox(txtSearchItem.Text & " was found, with a mark of " & frmStudentDatabase.ListView1.Items(txtSearchItem.Text).SubItems(1).Text)
        Else
            MsgBox(txtSearchItem.Text & " was not found in the Student Database")
        End If

I think the line of code :
VB
MsgBox(txtSearchItem.Text & " was found, with a mark of " & frmStudentDatabase.ListView1.Items(txtSearchItem.Text).SubItems(1).Text)
is wrong, however I am not sure how to display the subitem of the item properly.
Posted
Updated 25-Dec-12 19:46pm
v6
Comments
Orcun Iyigun 26-Dec-12 1:47am    
Have you debugged it yet and see what values you are getting?
AnnuBhai 28-Feb-13 2:33am    
good searching code thanx

1 solution

You can start thinking of the problem you've mentioned in the following line:
VB
MsgBox(txtSearchItem.Text & " was found, with a mark of " & frmStudentDatabase.ListView1.Items(txtSearchItem.Text).SubItems(1).Text)


if your code passes successfully the condition to get to execute the line for showing a message including the student mark then it means you don't have to ask the ListView1 to find the same item again, right?
P.S. when you ask ListView1.Items() to find an item for you you should either use an Integer (Indicating the index of the item you're looking for) or a String (Indicating the key [Name] of the item you're looking for) so both of them have nothing to do with the item's text and so if the items key or name is not set then ListView1.Items(String) won't give result.
now you can modify the conditional block statements above to be like this:
VB
Me.Hide()
frmStudentDatabase.Show()
' I just replaced frmStudentDatabase.ListView1.Items(txtSearchItem.Text).SubItems(1) with SearchItem.SubItems(1).Text
' and inserted a focus call function to get ListView1 focused |after| showing the message
' focused so when SearchItem is selected, is apparently selected too.
MsgBox(txtSearchItem.Text & " was found, with a mark of " & SearchItem.SubItems(1).Text)
frmStudentDatabase.ListView1.Focus()
SearchItem.Selected = True


Test it and get back to us, if nothing seems to happen then please add a msgbox call function before hiding the current form, in code:

Add something like -> MsgBox("Found An Item!")
Before this line -> Me.Hide()


Good luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900