在列表视图中搜索项目并显示自己和子项 - VB [英] Searching through a listview for an item and displaying itself and subitems - VB

查看:77
本文介绍了在列表视图中搜索项目并显示自己和子项 - VB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,教师输入学生姓名和相应的标记。我已经完成了以单独的形式将数据输入到列表视图中,但是我在创建搜索表单时遇到问题,搜索表单搜索输入的学生姓名,并在消息框中显示学生姓名是否与他们的标记一致输入,或未找到名称。通过将搜索项输入文本框,然后单击名为search的按钮完成搜索,其中在列表视图中,0是学生姓名项,子项(1)是学生标记。



到目前为止,我有以下代码:

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:

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 :

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.

推荐答案

你可以开始考虑你在下面一行中提到的问题:

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





如果您的代码成功通过条件以执行显示包含学生标记的消息的行,那么这意味着您不必再要求ListView1再次找到相同的项目,对吧? />
PS 当您要求 ListView1.Items()为您查找项目时,您应该使用整数(表示您要查找的项目的索引)或字符串(表示您要查找的项目的键[名称])所以这两个与项目的文本无关,因此如果未设置项目键或名称,则 ListView1.Items( String 不会给出结果。

现在您可以修改上面的条件块语句,如下所示:



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:

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





测试并回复我们,如果似乎没有任何结果,请添加一个msgbox在隐藏当前表单之前调用函数,在代码中:





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


这篇关于在列表视图中搜索项目并显示自己和子项 - VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆