文本框保存和显示从.txt文件

本文关键字:txt 文件 显示 保存 文本 | 更新日期: 2023-09-27 18:01:40

我有一个WinForm,每行有15个文本框,每个文本框旁边都有一个保存和显示按钮。用户可以在这些框中写入文本,当他们打开表单时,他们可以点击显示,查看他们写的文本,然后保存并关闭应用程序,它仍然在那里显示。

对于代码,我认为我可以在每个文本框和按钮上单独使用

namespace UniversityProject
{
public partial class managementsystem : Form
{
    private const string fileName = @"C:'txtBoxdata.txt";
    public managementsystem()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        textBox3.Text = File.ReadAllText(fileName);
    }
    private void button2_Click(object sender, EventArgs e)
    {
        File.WriteAllText(fileName, textBox3.Text);
    }

我认为它会通过改变文本框的数字工作,但我认为它可能有一些东西与阅读所有。由于所有的文本框显示相同的数据,这是不正确的

文本框保存和显示从.txt文件

让我试着澄清你的问题:

你有一堆文本框,每个文本框代表文本文件中的一行。每次用户单击文本框旁边的按钮时,您都希望替换相应的行。

替换文本文件中的单行是很容易的。只需将所有行读入数组,替换行并将所有内容写回文件:

private static void ReplaceLineInFile(string path, int lineNumber, string newLine)
{
    if (File.Exists(path))
    {
        string[] lines = File.ReadAllLines(path);
        lines[lineNumber] = newLine;
        File.WriteAllLines(path, lines);
    }
}

唯一剩下的就是知道哪一行应该被替换。您可以为每个按钮添加一个处理程序(注意行号以0开头):

private void button1_Click(object sender, EventArgs e)
{
    ReplaceLineInFile(fileName, 0, textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
    ReplaceLineInFile(fileName, 1, textBox2.Text);
}
etc.

这不是很优雅,因为它重复了相同的代码。最好是对所有按钮使用单个事件处理程序,然后找出它所处理的文本框以及应该替换的行。我建议为文本框和按钮设置数组,并在构造函数中构建它们:

private TextBox[] textBoxes;
private Button[] buttons;
public managementsystem()
{
    InitializeComponent();
    textBoxes = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5 };
    buttons = new Button[] { button1, button2, button3, button4, button5 };
}

您的单个事件处理程序将是:

private void button_Click(object sender, EventArgs e)
{
    Button button = sender as Button;
    if (button != null)
    {
        int lineNumber = Array.IndexOf(buttons, button);
        if (lineNumber >= 0)
        {
            ReplaceLineInFile(fileName, lineNumber, textBoxes[lineNumber].Text);
        }
    }
}

在某些时候,您可能希望保存所有值和/或创建文件。此外,您可能希望在加载表单时将现有值加载到文本框中:

private void Form1_Load(object sender, EventArgs e)
{
    LoadFile();
}
private void LoadFile()
{
    if (!File.Exists(fileName))
    {
        WriteAllLines();
        return;
    }
    string[] lines = File.ReadAllLines(fileName);
    if (lines.Length != textBoxes.Length)
    {
        // the number of lines in the file doesn't fit so create a new file
        WriteAllLines();
        return;
    }
    for (int i = 0; i < lines.Length; i++)
    {
        textBoxes[i].Text = lines[i];
    }
}
private void WriteAllLines()
{
    // this will create the file or overwrite an existing one
    File.WriteAllLines(fileName, textBoxes.Select(tb => tb.Text));
}

注意,当你添加新的文本框时,这仍然会起作用。唯一需要更改的是在构造函数中创建数组。但是,如果更改文本框的数量,则会删除现有文件。为了避免这种情况,您可以手动添加或删除新行。

基本问题是您试图将不同的文本框值保存到一个txt文件中。将txt文件名称与文本框名称一起更改,然后就可以了。:)

获取StreamWriterStreamReader。在OnClick-events中使用流写入器或阅读器打开一个新流。然后使用

using(StreamWriter sw = new StreamWriter("C:''Path''to''file.txt")){
    sw.WriteLine("TEXTBOX1: " + textbox1.text);
    sw.Close();
} 

对所有的文本框都这样做。最后使用StreamReader从文件

中读取
using(StreamReader sr = new StreamReader("C:''Path''to''file.txt")){
    string s  = sr.ReadToEnd();
    //find value for your textbox;
    textbox1.Text = s;
    sw.Close();
} 

试一试吧!