Search code examples
c#formsvisual-studiofilestream

load from a txt to a list box in C#


I already have the saving part down and I know it works, but when I click load button, it will not display anything that I have saved from the text boxes that go to the saying.txt file

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
        {

        }
    }


}
}

any reason why it is not loading into the list box? I have tried both label and text box to display the message and neither of them work. I debugged the program and it is executing fine


Solution

  • You have multiple issues here but I will point out two main issues that will get your code working.

    1. You are not closing the stream when you try to save. If the stream stays open, you will never be able to read the values when you try to "Load" the file. You need to call fil.Close(); at the end of your btnSaveAs_Click method.

    This is how the save should read.

        fil.WriteLine(txtBxLastName.Text);
        txtBxLastName.Text = "";
        txtBxLastName.Focus();
    
        fil.Close();
    
    1. You're skipping the first line of the file in your "Load" method. You call infil.ReadLine(); then in the loop, you call it again before you add it to your listbox. You need to move your second ReadLine(). If you are only ever writing a single line to the file, your existing code will skip that first line and try to read it again which will be null (no second line). So, it will never add anything to your listbox.

    This is how the reads should be ordered.

        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
    

    Those changes will get you working. Now to point out, you are going about reading and writing to a file all wrong. You should not be opening a stream in your form load and waiting for button clicks to be writing/reading from that stream. Unless you have a very good reason to do what you are doing, you should be opening your stream, performing the read/write operation, and closing your stream right away. For a simple file IO, I would even suggest using a different mechanism. Look at the MSDN for the System.IO.File Class for easier methods to read lines or write lines to a file.