2016-04-25 28 views
0

我已经有保存部分了,我知道它的工作原理,但是当我点击加载按钮时,它不会显示任何我已经从文本框中保存的文本框.txt文件从txt加载到C#列表框中的

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 
     { 

     } 
    } 


} 
} 

为什么不加载到列表框中的任何原因?我已经尝试过标签和文本框来显示消息,但他们都没有工作。我调试了程序,它执行得很好

回答

0

您有多个问题,但我会指出两个会让您的代码正常工作的主要问题。

  1. 当您尝试保存时,您未关闭流。如果流保持打开状态,当您尝试“加载”文件时,您将永远无法读取这些值。您需要在btnSaveAs_Click方法结束时致电fil.Close();

这是如何保存应该阅读。

fil.WriteLine(txtBxLastName.Text); 
    txtBxLastName.Text = ""; 
    txtBxLastName.Focus(); 

    fil.Close(); 
  • 你跳过该文件的第一行中的“加载”的方法。然后在循环中调用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以获取更简单的方法来读取行或将文本写入文件。

    +0

    我得到了fil.Close();部分在您重新发布代码的第二部分之前,感谢您的建议。但是,我尝试了一个txtBxFirstName,并且它显示的是txtBxLastName。尝试过fil.Close后txtBxFirstName和之前和两者 – Joe

    +0

    请参阅第2部分,因为它只显示'txtBxLastName'。您的原始代码正在读取(获取第一行),然后检查null,然后输入您的循环。此时,在将第一行添加到列表框之前,您需要进行第二次阅读。所以,你基本上跳过你的第一次阅读。看看在循环中,我如何切换'lstBxDisplay.Items.Add(inValue)'和'inValue = infil.ReadLine()'的位置。 – Mikanikal