从文本加载到 C# 中的列表框
load from a txt to a list box in C#
我已经保存了保存部分,我知道它可以工作,但是当我单击加载按钮时,它不会显示我从转到 saying.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
{
}
}
}
}
为什么它没有加载到列表框中?我已经尝试过标签和文本框来显示消息,但它们都不起作用。我调试了程序,它运行良好
你在这里有多个问题,但我会指出两个主要问题,这将使你的代码正常工作。
- 您尝试保存时并未关闭流。如果流保持打开状态,您将永远无法在尝试 "Load" 文件时读取值。您需要在
btnSaveAs_Click
方法的末尾调用 fil.Close();
。
存档应该是这样的。
fil.WriteLine(txtBxLastName.Text);
txtBxLastName.Text = "";
txtBxLastName.Focus();
fil.Close();
- 您在 "Load" 方法中跳过了文件的第一行。您调用
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
这些改变会让你工作。现在要指出的是,您将要读取和写入一个文件是完全错误的。您不应该在表单加载中打开流并等待按钮点击来自该流的 writing/reading。 除非你有充分的理由做你正在做的事情,否则你应该打开你的流,执行 read/write 操作,然后立即关闭你的流。对于简单的文件 IO,我什至建议使用不同的机制。查看 MSDN for the System.IO.File Class 以获取更简单的方法来读取行或将行写入文件。
我已经保存了保存部分,我知道它可以工作,但是当我单击加载按钮时,它不会显示我从转到 saying.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
{
}
}
}
}
为什么它没有加载到列表框中?我已经尝试过标签和文本框来显示消息,但它们都不起作用。我调试了程序,它运行良好
你在这里有多个问题,但我会指出两个主要问题,这将使你的代码正常工作。
- 您尝试保存时并未关闭流。如果流保持打开状态,您将永远无法在尝试 "Load" 文件时读取值。您需要在
btnSaveAs_Click
方法的末尾调用fil.Close();
。
存档应该是这样的。
fil.WriteLine(txtBxLastName.Text);
txtBxLastName.Text = "";
txtBxLastName.Focus();
fil.Close();
- 您在 "Load" 方法中跳过了文件的第一行。您调用
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
这些改变会让你工作。现在要指出的是,您将要读取和写入一个文件是完全错误的。您不应该在表单加载中打开流并等待按钮点击来自该流的 writing/reading。 除非你有充分的理由做你正在做的事情,否则你应该打开你的流,执行 read/write 操作,然后立即关闭你的流。对于简单的文件 IO,我什至建议使用不同的机制。查看 MSDN for the System.IO.File Class 以获取更简单的方法来读取行或将行写入文件。