English 中文(简体)
如何搜查名单上一栏的具体物品?
原标题:How to search a specific item of a column in ListView?
  • 时间:2013-10-10 11:20:45
  •  标签:
  • vb.net

在我的清单中,有4栏,有文本Box。 如何让用户在清单调查第二栏中搜索插图?

问题回答
Private Function getListItemByName(query As String) As ListViewItem

For Each lvi As ListViewItem In lst.Items

  If lvi.Text.Equals(query) Then Return lvi

  For Each si As ListViewItem.ListViewSubItem In lvi.SubItems

  If si.Text.Equals(query) Then Return lvi

  Next

Next

Return Nothing

End Function

http://www.dreamincode.net/forums/topic/237883-search-for-an-item-in-listview/

一种办法是使用准则:

Public Function SearchLV(lv As ListView, SearchString As String, Colmn As Integer) As List(Of ListViewItem)
    SearchLV = (From item In lv.Items.OfType(Of ListViewItem)()
            Where item.SubItems(Colmn).Text = SearchString
            Select item).ToList
    If SearchLV.Count = 0 Then
        SearchLV.Add(New ListViewItem("Your search returned no results"))
    End If
End Function

这项职能将回到一个具体栏目中与搜索术语相对应的清单。 栏目为零基指数。 如果清单上没有项目,第一个项目将包含这一信息。

仅通过清单检索这些物品:

Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
    Dim searchstring As String = TextBox1.Text
    Dim searchlist As List(Of ListViewItem) = Search(ListView1, searchstring, 2)
    Dim mssage As String = ""
    For Each item In searchlist
        For Each subitem As ListViewItem.ListViewSubItem In item.SubItems
            mssage += subitem.Text + "-"
        Next
        mssage = mssage.Substring(0, mssage.Length - 1) + vbNewLine
    Next
    MessageBox.Show(mssage)
End Sub

如果你想要部分改写字:

Where item.SubItems(Colmn).Text = SearchString

:

Where item.SubItems(Colmn).Text.Contains(SearchString)

This is what works for me it s easy to understand and easy to modify. With this you can search specifified rows in listview.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click       

   For Each info As ListViewItem In ListView1.Items

                    Dim NameCheck As String = 0
                    Dim SurnameCheck As String = 0
                    Dim IDCheck As String = 0

                    NameCheck = info.Text
                    SurnameCheck = info.SubItems(1).Text
                    IDCheck = info.SubItems(2).Text

                    If NameCheck = TextBox1.Text Then
                        If SurnameCheck = TextBox2.Text Then
                            If IDCheck = TextBox3.Text Then

                                MessageBox.Show("User is already in database!!!", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                                GoTo ErrorReturn  execute difference piece of code

                            End If
                        End If
                    End If
                Next




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签