【问题标题】:load from a txt to a list box in C#在 C# 中从文本加载到列表框
【发布时间】:2016-04-25 09:51:05
【问题描述】:

我已经关闭了保存部分,我知道它可以工作,但是当我单击加载按钮时,它不会显示我从 say.txt 文件的文本框中保存的任何内容

using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Grades : Form
{

    private StreamWriter fil;
    public Grades()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            fil = new StreamWriter("saying.txt"); //This is the txt file
        }
        catch (DirectoryNotFoundException exc)
        {
            lstBxDisplay.Text = "Nothing " +
                exc.Message;
        }
        catch (System.IO.IOException exc)
        {
            lstBxDisplay.Text = exc.Message;
        }
    }      

    // saving the files to the saying.txt 
    private void btnSaveAs_Click(object sender, EventArgs e)
    {
        try
        {
            fil.WriteLine(txtBxLastName.Text);
            txtBxLastName.Text = "";
            txtBxLastName.Focus();
        }
        catch (System.IO.IOException exc)
        {
            lstBxDisplay.Text = exc.Message;
        }
    }

    // next is the load button to load the files into the list/display box
    private void btnLoad_Click(object sender, EventArgs e)
    {


        string inValue;

        try
        {
            using (StreamReader infil =
                new StreamReader("saying.txt"))
            {
                inValue = infil.ReadLine();
                while (inValue != null)
                {
                    inValue = infil.ReadLine();
                    if (inValue != null)
                        this.lstBxDisplay.Items.Add(inValue);
                } // end of while
            } // end of using
        }
        catch (System.IO.IOException exc)
        {
            lstBxDisplay.Text = exc.Message;
        }
    }

    private void Grades_FormClosing(object sender, FormClosingEventArgs e)
    {
        try
        {
            fil.Close();
        }
        catch
        {

        }
    }


}
}

它没有加载到列表框中的任何原因?我已尝试使用标签和文本框来显示消息,但它们都不起作用。我调试了程序,它运行良好

【问题讨论】:

    标签: c# forms visual-studio filestream


    【解决方案1】:

    您在这里有多个问题,但我将指出两个主要问题,这些问题将使您的代码正常工作。

    1. 尝试保存时没有关闭流。如果流保持打开状态,当您尝试“加载”文件时,您将永远无法读取这些值。您需要在 btnSaveAs_Click 方法的末尾调用 fil.Close();

    这就是存档的读取方式。

        fil.WriteLine(txtBxLastName.Text);
        txtBxLastName.Text = "";
        txtBxLastName.Focus();
    
        fil.Close();
    
    1. 您在“加载”方法中跳过了文件的第一行。您调用 infil.ReadLine(); 然后在循环中,在将其添加到列表框之前再次调用它。你需要移动你的第二个ReadLine()。如果您只向文件写入一行,则现有代码将跳过第一行并尝试再次读取它,这将为空(没有第二行)。因此,它永远不会向您的列表框添加任何内容。

    这就是读取的顺序。

        using (StreamReader infil = new StreamReader("saying.txt"))
        {
            inValue = infil.ReadLine();
    
            while (inValue != null)
            {
                this.lstBxDisplay.Items.Add(inValue);
    
                inValue = infil.ReadLine();
    
            } // end of while
        } // end of using
    

    这些更改将使您工作。现在要指出的是,您正在阅读和写入文件都是错误的。您不应该在表单加载中打开流并等待按钮单击从该流中写入/读取。 除非你有充分的理由去做你正在做的事情,否则你应该打开你的流,执行读/写操作,然后立即关闭你的流。对于简单的文件 IO,我什至建议使用不同的机制。查看MSDN for the System.IO.File Class 以获得更简单的方法来读取行或将行写入文件。

    【讨论】:

    • 我得到了 fil.Close();在您重新发布代码的第二部分之前,感谢您的建议。但是,我尝试使用 txtBxFirstName,它显示的只是 txtBxLastName。在 txtBxFirstName 之后和之前和两者都尝试了 fil.Close
    • 请参阅第 2 部分,因为它只显示 txtBxLastName。您的原始代码正在读取(以获取第一行)并且您检查 null 然后进入您的循环。此时,您在将第一行添加到列表框之前进行第二次阅读。所以,你基本上跳过了你的第一次阅读。看看我如何在循环中切换lstBxDisplay.Items.Add(inValue)inValue = infil.ReadLine() 的位置。
    猜你喜欢
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多