簡體   English   中英

Listview-選擇與值最相似的項目

[英]Listview - select mosti similar item to value

如何在列表視圖(第一列)中選擇一個最類似於標簽或文本框中的字符串值的項目。

Listview填充以下代碼:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ListView1.Items.Clear()
    ListView1.View = System.Windows.Forms.View.Details
    ListView1.Columns.Add("COL1", 100, HorizontalAlignment.Left) 'KONTO
    ListView1.Columns.Add("COL2", 140, HorizontalAlignment.Left) 'NAZIV

    Dim FilePath As String = "W:\GLAVNI\KOR14\"
    Dim DBF_File As String = "MATIKGL"
    Dim ColName As String = "KONTO"
    'Dim naz As String
    Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _
    " ;Extended Properties=dBASE IV")
        con.Open()
        Using cmd As New OleDbCommand("SELECT * FROM MATIKGL ORDER BY KONTO, NAZIV", con)
            Using reader As OleDbDataReader = cmd.ExecuteReader()
                If reader.HasRows Then
                    While (reader.Read())
                        Me.ListView1.Items.Add(reader("KONTO"))
                        'ListView1.Items(i).SubItems.Add(rdr.Item("YourColumnName").ToString)
                        'BELOW SELECTS ALL ITEMS THAT STARTS WITH 2020-
                        For i = 0 To ListView1.Items.Count - 1
                            If ListView1.Items(i).ToString.Contains("2020-") Then

                            Else
                                ListView1.Items.Remove(ListView1.Items(i))
                            End If
                        Next

                    End While

                Else

                End If
            End Using
        End Using
        con.Close()
    End Using
End Sub

我有一個文本框和一個按鈕。 來自文本框的文本輸入應與列表視圖中的所有項目進行比較,並應選擇最接近的項目。 還有一件事:所有項目均按字母順序排序

按鈕代碼為:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ListView1.MultiSelect = False
    ListView1.FullRowSelect = True

    Dim checkInt As Integer = FindItem(ListView1, "2020-" & TextBox1.Text)'this one is changed since all items starts with "2020-"& UCASE TEXT
    If checkInt <> -1 Then

        ListView1.Items(checkInt).Selected = True
        ListView1.Focus()

    Else
        Label1.Text = "Search string not found"
    End If
End Sub

更新的代碼

            Dim checkInt As Integer = FindItem(ListView1, "2020-" & TextBox3.Text)

        If checkInt <> -1 Then

            TextBox4.Focus()

        Else
            Label14.Text = "NEMA"
            On Error GoTo ext
            Dim li As ListViewItem
            ListView1.SelectedItems.Clear()
            ListView1.HideSelection = False

            li = ListView1.FindItemWithText("2020-" & UCase(TextBox3.Text))

            If Not (li Is Nothing) Then
                Me.ListView1.Focus()
                li.Selected = True
                li.EnsureVisible()
            ElseIf li Is Nothing Then
                li = ListView1.FindItemWithText("2020-" & Strings.Left(TextBox3.Text, 1))
                Me.ListView1.Focus()
                li.Selected = True
                li.EnsureVisible()
            Else

            End If

            Exit Sub
ext:

            TextBox3.Text = ""
            TextBox3.Focus()
            Label14.Text = "String not found"



        End If

這個作品。

我知道這不是最好的解決方案,但它正在起作用。 可以在沒有您幫助的情況下解決此問題,謝謝Phillip Trelford

定義一個函數來對兩個字符串的緊密度進行評分,然后使用LINQ查找最低得分,即

' Example score function
Function Score(a As String, b As String) As Integer
    Dim index = 0
    While index < a.Length And index < b.Length
        Dim diff = Math.Abs(AscW(a(index)) - AscW(b(index)))
        If diff <> 0 Then Return diff
        index += 1
    End While
    Return Math.Abs(a.Length - b.Length)
End Function

Function Closest(searchWord As String, words As String()) As String
    Dim ordered =
        From w In words
        Select Word = w, Score = Score(w, searchWord)
        Order By Score

    Return ordered.First().Word
End Function

Sub Main()
    Dim words = {"Alpha", "Apple", "Ask"}
    Dim searchWord = "Ann"
    Dim word = Closest(searchWord, words)
    Console.WriteLine(word)
End Sub

更新資料

要在WinForms ListView中選擇值,您需要大致執行以下操作:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListView1.MultiSelect = False
ListView1.FullRowSelect = True

Dim prefix = "2020-"
' Extract items from Listview
Dim items = New List(Of String)()
For Each item In ListView1.Items
    items.Add(item)
Next
Dim words = items.ToArray()
Dim searchWord = TextBox1.Text
Dim resultWord = Closest(searchWord, words)
'this one is changed since all items starts with "2020-"& UCASE TEXT
Dim checkInt As Integer = FindItem(ListView1, prefix & resultWord) 
If checkInt <> -1 Then

    ListView1.Items(checkInt).Selected = True
    ListView1.Focus()

Else
    Label1.Text = "Search string not found"
End If
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM