比较两个列表框并显示差异(VB.net) [英] Comparing two list box and displaying the differences (VB.net)

查看:131
本文介绍了比较两个列表框并显示差异(VB.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我的应用程序遇到了麻烦.我试图将一个列表加载到listbox1中,然后刷新listbox2中的相同列表(但结果可能不同),然后将两者进行比较,并在textbox1中显示两个列表框之间的差异.我已经知道了是否存在差异,但是当它发布到文本框中时,它将显示整个列表框,而不是差异.

Hello I am having troubles with my application. I am trying to load a list into listbox1 and then refresh the same list in listbox2 (but with possibly different results) and then compare the two and display in textbox1 the differences between the two list boxes. I have gotten to a point where I am able to tell if there are differences but when it goes to post into the textbox it displays the entire listbox and not the differences.

有点罗word.对不起.下面是我的代码:

That's a little wordy. Sorry. Below is my code:

 TextBox1.Text = ""
    Dim Folder As String = My.Settings.path 
    ListBox2.Items.Clear()
    For Each File As String In My.Computer.FileSystem.GetFiles _
                                                (Folder, FileIO.SearchOption.SearchAllSubDirectories)
        ListBox2.Items.Add(IO.Path.GetFileName(File)) 
    Next

'这是问题所在-系统比较这些项目并在文本框中显示所有项目.

' This is where the issues is - The system compares the items and displays all items in the textbox.

For Each item In ListBox1.Items
        If item.ToString = ListBox2.Items.ToString Then

        Else
            TextBox1.Text += (Environment.NewLine + item.ToString)
        End If
    Next

感谢您的帮助.

推荐答案

您可以使用LINQ.本示例将找到ListBox1中的所有项目,而不是ListBox2中的所有项目:

You can use LINQ. This example will find all items in ListBox1 not in ListBox2:

Dim result As List(Of String) = (From s1 As String In Me.ListBox1.Items Where Not Me.ListBox2.Items.Contains(s1) Select s1).ToList()

Me.TextBox1.Text = String.Join(Environment.NewLine, result)

这篇关于比较两个列表框并显示差异(VB.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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