C# 从文件加载列表框

C# 从文件加载列表框,c#,winforms,listbox,C#,Winforms,Listbox,这是我第一次创建C#程序,如果这个问题看起来很基本,我很抱歉。我的设计表单上有3个列表框和3个按钮,我想在单击列表框对应的按钮时将项目列表加载到每个文本框中。有人能告诉我怎么做吗。以下是在列表框中加载文本文件的步骤 逐行读取文本文件 读取时,填充列表框 下面是一个关于如何做到这一点的小示例: string line; var file = new System.IO.StreamReader("C:\\PATH_TO_FILE\\test.txt"); while ((line = file.R

这是我第一次创建C#程序,如果这个问题看起来很基本,我很抱歉。我的设计表单上有3个列表框和3个按钮,我想在单击列表框对应的按钮时将项目列表加载到每个文本框中。有人能告诉我怎么做吗。

以下是在列表框中加载文本文件的步骤

  • 逐行读取文本文件
  • 读取时,填充列表框
  • 下面是一个关于如何做到这一点的小示例:

    string line;
    var file = new System.IO.StreamReader("C:\\PATH_TO_FILE\\test.txt");
    while ((line = file.ReadLine()) != null)
    {
        listBox1.Items.Add(line);
    }
    

    以下是在列表框中加载文本文件的步骤

  • 逐行读取文本文件
  • 读取时,填充列表框
  • 下面是一个关于如何做到这一点的小示例:

    string line;
    var file = new System.IO.StreamReader("C:\\PATH_TO_FILE\\test.txt");
    while ((line = file.ReadLine()) != null)
    {
        listBox1.Items.Add(line);
    }
    

    您只需为每个按钮创建一个事件处理程序。 您可以通过双击VisualStudio设计器上的按钮来完成此操作。 然后您将看到代码窗口,其中创建了以下新代码

    private void button1_Click(object sender, EventArgs e)
    {
    
    }
    
    在此方法上,实现项目加载方法并将它们添加到ListBox.Items中。 例如:

    private void button1_Click(object sender, EventArgs e)
    {
    string[] allLines = File.ReadAllLines(@"C:\Test.txt"); // reads all lines from text file
    listBox1.AddRange(allLines); // Adds an array of objects into the ListBox's Item Collection.
    }
    

    希望对你有帮助,祝你好运

    您只需为每个按钮创建一个事件处理程序。 您可以通过双击VisualStudio设计器上的按钮来完成此操作。 然后您将看到代码窗口,其中创建了以下新代码

    private void button1_Click(object sender, EventArgs e)
    {
    
    }
    
    在此方法上,实现项目加载方法并将它们添加到ListBox.Items中。 例如:

    private void button1_Click(object sender, EventArgs e)
    {
    string[] allLines = File.ReadAllLines(@"C:\Test.txt"); // reads all lines from text file
    listBox1.AddRange(allLines); // Adds an array of objects into the ListBox's Item Collection.
    }
    

    希望对你有帮助,祝你好运

    阿巴斯给了你一个充分的答案,但有几个问题,所以我想我会补充我自己的回答。这些问题:

  • 流(任何实现了
    IDisposable
    )的东西)需要在处理完后关闭。您可以通过手动调用
    Dispose()
    或使用块将对象的创建包装在
    中来完成此操作
  • 如果列表框中有大量项目,则不应像这样逐个向列表框中添加项目。这将导致性能不佳,并且列表框在添加每个项目后更新/重画时将闪烁
  • 我会这样做:

    using System.IO;
    // other includes
    
    public partial class MyForm : Form
    {
        public MyForm()
        {
            // you can add the button event 
            // handler in the designer as well
            someButton.Click += someButton_Click;
        }
    
        private void someButton_Click( object sender, EventArgs e )
        {
            PopulateList( "some file path here" );
        }
    
        private void PopulateList( string filePath )
        {
            var items = new List<string>();
            using( var stream = File.OpenRead( filePath ) )  // open file
            using( var reader = new TextReader( stream ) )   // read the stream with TextReader
            {
                string line;
    
                // read until no more lines are present
                while( (line = reader.ReadLine()) != null )
                {
                    items.Add( line );
                }
            }
    
            // add the ListBox items in a bulk update instead of one at a time.
            listBox.AddRange( items );
        }
    }
    
    使用System.IO;
    //其他包括
    公共部分类MyForm:Form
    {
    公共MyForm()
    {
    //您可以添加按钮事件
    //设计器中的处理程序
    someButton.Click+=someButton\u Click;
    }
    私有void someButton_单击(对象发送者,事件参数e)
    {
    PopulateList(“此处的某些文件路径”);
    }
    私有void PopulateList(字符串文件路径)
    {
    var items=新列表();
    使用(var stream=File.OpenRead(filePath))//打开文件
    使用(var reader=newTextReader(stream))//使用TextReader读取流
    {
    弦线;
    //阅读,直到没有更多的行出现
    而((line=reader.ReadLine())!=null)
    {
    项目。添加(行);
    }
    }
    //在批量更新中添加列表框项目,而不是一次添加一个。
    listBox.AddRange(项目);
    }
    }
    
    阿巴斯给了你一个充分的答案,但有几个问题,所以我想我会补充我自己的回答。这些问题:

  • 流(任何实现了
    IDisposable
    )的东西)需要在处理完后关闭。您可以通过手动调用
    Dispose()
    或使用
    块将对象的创建包装在
    中来完成此操作
  • 如果列表框中有大量项目,则不应像这样逐个向列表框中添加项目。这将导致性能不佳,并且列表框在添加每个项目后更新/重画时将闪烁
  • 我会这样做:

    using System.IO;
    // other includes
    
    public partial class MyForm : Form
    {
        public MyForm()
        {
            // you can add the button event 
            // handler in the designer as well
            someButton.Click += someButton_Click;
        }
    
        private void someButton_Click( object sender, EventArgs e )
        {
            PopulateList( "some file path here" );
        }
    
        private void PopulateList( string filePath )
        {
            var items = new List<string>();
            using( var stream = File.OpenRead( filePath ) )  // open file
            using( var reader = new TextReader( stream ) )   // read the stream with TextReader
            {
                string line;
    
                // read until no more lines are present
                while( (line = reader.ReadLine()) != null )
                {
                    items.Add( line );
                }
            }
    
            // add the ListBox items in a bulk update instead of one at a time.
            listBox.AddRange( items );
        }
    }
    
    使用System.IO;
    //其他包括
    公共部分类MyForm:Form
    {
    公共MyForm()
    {
    //您可以添加按钮事件
    //设计器中的处理程序
    someButton.Click+=someButton\u Click;
    }
    私有void someButton_单击(对象发送者,事件参数e)
    {
    PopulateList(“此处的某些文件路径”);
    }
    私有void PopulateList(字符串文件路径)
    {
    var items=新列表();
    使用(var stream=File.OpenRead(filePath))//打开文件
    使用(var reader=newTextReader(stream))//使用TextReader读取流
    {
    弦线;
    //阅读,直到没有更多的行出现
    而((line=reader.ReadLine())!=null)
    {
    项目。添加(行);
    }
    }
    //在批量更新中添加列表框项目,而不是一次添加一个。
    listBox.AddRange(项目);
    }
    }
    
    试试这个例子,记住包括System.IO

            private void button3_Click(object sender, EventArgs e)
        {
            //Pass the file path and file name to the StreamReader constructor
            StreamReader sr = new StreamReader("youfilePath");
            string line = string.Empty;
            try
            {
                //Read the first line of text
                line = sr.ReadLine();
                //Continue to read until you reach end of file
                while (line != null)
                {
                    this.listBox1.Items.Add(line);
                    //Read the next line
                    line = sr.ReadLine();
                }
    
                //close the file
                sr.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
            finally
            {
                //close the file
                sr.Close();
            }
        }
    
    使用System.IO

            private void button3_Click(object sender, EventArgs e)
        {
            //Pass the file path and file name to the StreamReader constructor
            StreamReader sr = new StreamReader("youfilePath");
            string line = string.Empty;
            try
            {
                //Read the first line of text
                line = sr.ReadLine();
                //Continue to read until you reach end of file
                while (line != null)
                {
                    this.listBox1.Items.Add(line);
                    //Read the next line
                    line = sr.ReadLine();
                }
    
                //close the file
                sr.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
            finally
            {
                //close the file
                sr.Close();
            }
        }
    

    问候。

    尝试这个例子,记住包括System.IO

            private void button3_Click(object sender, EventArgs e)
        {
            //Pass the file path and file name to the StreamReader constructor
            StreamReader sr = new StreamReader("youfilePath");
            string line = string.Empty;
            try
            {
                //Read the first line of text
                line = sr.ReadLine();
                //Continue to read until you reach end of file
                while (line != null)
                {
                    this.listBox1.Items.Add(line);
                    //Read the next line
                    line = sr.ReadLine();
                }
    
                //close the file
                sr.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
            finally
            {
                //close the file
                sr.Close();
            }
        }
    
    使用System.IO

            private void button3_Click(object sender, EventArgs e)
        {
            //Pass the file path and file name to the StreamReader constructor
            StreamReader sr = new StreamReader("youfilePath");
            string line = string.Empty;
            try
            {
                //Read the first line of text
                line = sr.ReadLine();
                //Continue to read until you reach end of file
                while (line != null)
                {
                    this.listBox1.Items.Add(line);
                    //Read the next line
                    line = sr.ReadLine();
                }
    
                //close the file
                sr.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
            finally
            {
                //close the file
                sr.Close();
            }
        }
    

    问候。

    请告诉我们。你自己走了多远?你在哪里卡住了?我刚学了Visual C#2010,在书中找不到关于如何填充列表框的示例。这些介绍书只展示了基本知识。我需要更进一步的消息告诉我们。你自己走了多远?你在哪里卡住了?我刚学了Visual C#2010,在书中找不到关于如何填充列表框的示例。这些介绍书只展示了基本知识。关于关闭读卡器,我需要了解一些更先进的东西:我没有把它放在那里,因为它只是一个小例子,但是关于关闭读卡器的“using”-语句,你是对的。而且我不知道一次性阅读文件会有这么高的效率,谢谢你的解释!)@阿巴斯:是的,我本想把它作为一个评论,但决定留下一个答案。我想你忽略了它,因为它只是一个简单的例子,但由于OP是一个初学者,我认为最好也解释一下这一部分。你解释得很好。另外,我也学到了一些新东西!:)@阿巴斯:很高兴我能帮上点忙