如何在列表视图VB.NET中搜索并显示相同的项目或重复的项目

[ad_1]

你好,

需要帮助,首先英语不好,
我有列表视图显示指定目录(文件夹)中文本文件中的项目,我想要搜索框和(TextBox1)和cmdButton(Button1)来搜索并显示我在第三列或ID号中搜索的所有项目。

前任:

第一栏:日期和时间:
第 2 栏:名称:
第 3 栏:身份证号码:

2017 年 8 月 8 日 06:06:00,约翰·卢卡斯,1010000
2017年8月8日 07:00:00, 四月邓, 1010023
08-08-2017 08:45:00,凯文·埃尔莫,1010014
2017 年 8 月 8 日 09:06:00,约翰·卢卡斯,1010000
2017 年 8 月 8 日 10:24:00,艾德·奥利维亚,1010012
2017 年 8 月 8 日 11:47:00,约翰·卢卡斯,1010000
2017年8月8日12:56:00,丽莎·本,1010001

我想通过搜索ID号来显示John Lucas的数据(项目)并将其显示在列表视图(另一个列表视图)中

我尝试过的:

“这是仅从文本文件显示我的项目的代码,我不知道,我需要你的帮助,TIA。”

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load, MyBase.DockChanged
lvDETAILS.View = View.Details
        lvDETAILS.Columns.Add("Date/Time:", 220, HorizontalAlignment.Left)
        lvDETAILS.Columns.Add("Employee Name:", 280, HorizontalAlignment.Left)
        lvDETAILS.Columns.Add("ID Number:", 120, HorizontalAlignment.Left)
        lvDETAILS.Columns.Add("Department:", 250, HorizontalAlignment.Left)
        lvDETAILS.Columns.Add("User Type:", 200, HorizontalAlignment.Left)
        lvDETAILS.Columns.Add("ESD Check Result:", 200, HorizontalAlignment.Left)

        Dim File_Name As String = "D:\esdcheck\DET.dll"
        Dim objReader As New IO.StreamReader(File_Name)
        Dim newarray(6) As String
        Dim DateTime As String
        Dim Name As String
        Dim IDNum As String
        Dim Department As String
        Dim UserType As String
        Dim ESDResult As String

        Do While objReader.Peek <> -1
            newarray = objReader.ReadLine().Split(",")
            DateTime = newarray(0)
            Name = newarray(1)
            IDNum = newarray(2)
            Department = newarray(3)
            UserType = newarray(4)
            ESDResult = newarray(5)
            Dim x As Integer = lvDETAILS.Items.Add(New ListViewItem(New String() {DateTime})).Index 'Para sa Listview unang Column depende sa base sa index
            lvDETAILS.Items.Item(x).SubItems.Add(Name) 'yung 'x' po na yun ay number of index na nakaset per column., ilalagay sa second column depende sa index
            lvDETAILS.Items.Item(x).SubItems.Add(IDNum) 'sunod sunod lang po ang pag lagay depende sa dami ng column
            lvDETAILS.Items.Item(x).SubItems.Add(Department) 'yeah
            lvDETAILS.Items.Item(x).SubItems.Add(UserType) 'yeah
            lvDETAILS.Items.Item(x).SubItems.Add(ESDResult) 'yeah !!! eto na yung last!
End Sub

解决方案1

最好的解决方案是使用支持数据绑定和过滤的控件。 虽然这是可能的并且过于复杂,但 ListView 并不真正适合此目的。

然而,DataGridView 是为您想要实现的任务而设计的。 这篇 CodeProject 文章向您展示了如何实现您想要的目标: 详细的数据绑定教程[^]

解决方案2

感谢您的答复

我使用 Visual Studio 2017 VB.net – 它是 C#

解决方案3

网络
Imports System.Collections.Generic

Public Class Form1
    ' Dictionary to store unique items
    Dim itemsDictionary As New Dictionary(Of String, ListViewItem)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Initialize ListView columns
        ListView1.View = View.Details
        ListView1.Columns.Add("Date and Time", 200)
        ListView1.Columns.Add("Name", 150)
        ListView1.Columns.Add("ID Number", 100)

        ' Add sample data
        AddItem("08-08-2017 06:06:00", "John Lucas", "1010000")
        AddItem("08-08-2017 07:00:00", "April Dun", "1010023")
        AddItem("08-08-2017 08:45:00", "Kevin Elmo", "1010014")
        AddItem("08-08-2017 09:06:00", "John Lucas", "1010000")
        AddItem("08-08-2017 10:24:00", "Ed Olivia", "1010012")
        AddItem("08-08-2017 11:47:00", "John Lucas", "1010000")
        AddItem("08-08-2017 12:56:00", "Liza Ben", "1010001")
    End Sub

    Private Sub AddItem(dateTime As String, name As String, idNumber As String)
        ' Create ListViewItem
        Dim item As New ListViewItem({dateTime, name, idNumber})

        ' Check for duplicates
        Dim key As String = $"{dateTime}-{name}-{idNumber}"
        If Not itemsDictionary.ContainsKey(key) Then
            ' Add item to ListView and dictionary
            ListView1.Items.Add(item)
            itemsDictionary.Add(key, item)
        Else
            ' Handle duplicate (e.g., change color or other action)
            item.BackColor = Color.LightPink
        End If
    End Sub
End Class

[ad_2]

コメント

标题和URL已复制