在lisBox.1中导入Text.txt [英] Import Text.txt in lisBox.1

查看:86
本文介绍了在lisBox.1中导入Text.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是导入listbox1中的文本文件的代码,它可以正常工作。我需要检查listBox1中内容的代码...我的意思是排除listbox1中已有的行。谢谢。



This below is the code to import the Text file in listbox1, and it works. I need the code that checks the content in listBox1...I mean excluding the lines that are already in the listbox1. Thank you.

private void button1_Click(object sender, EventArgs e)
        {
          string sLine = "";
            using (StreamReader sr = new StreamReader(@"c:\Text.txt"))
            {
                while (!sr.EndOfStream)
                {
                    sLine = sr.ReadLine();
                    listBox1.Items.Add(sLine);
                    
                }
                
               MessageBox.Show("Completed..!");          

            }
        }    

推荐答案

可能的解决方案是:couple ListBox ,其中包含类 System.Collections.Generic.HashSet<> 的实例。您甚至可以创建一个派生自 ListView 的类。在每次添加或删除操作时,使集合的内容与项目同步。



为什么?因为找到项目的时间复杂度是该集合是O(1)。请参阅:

Big O表示法 - 维基百科,免费的百科全书 [ ^ ],

时间复杂度 - 维基百科,免费的百科全书 [ ^ ]。



-SA
On of the possible solution is: couple ListBox with the instance of the class System.Collections.Generic.HashSet<>. You can even create a class derived from ListView. On each add or remove operation, make the content of the set in sync with the items.

Why? Because the time complexity of finding of an item is the set is O(1). Please see:
Big O notation - Wikipedia, the free encyclopedia[^],
Time complexity - Wikipedia, the free encyclopedia[^].

—SA


我找到了一个办法。谢谢。

I found a solution. Thanks.
listBox1.Items.AddRange( File.ReadLines( @"C:\Text.txt" ).Distinct().Where( s => ! listBox1.Items.Contains( s )).ToArray() )


这篇关于在lisBox.1中导入Text.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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