Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文本框从.txt文件保存和显示_C#_Winforms_Textbox - Fatal编程技术网

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

C# 文本框从.txt文件保存和显示,c#,winforms,textbox,C#,Winforms,Textbox,我有一个WinForm,一行有15个文本框,每个文本框旁边都有一个保存和显示按钮。用户可以在这些框中写入文本,当他们打开表单时,他们可以单击“显示”并查看他们写入的文本,然后保存并关闭应用程序,并在其中显示 对于代码,我想我可以在每个文本框和按钮上单独使用它 namespace UniversityProject { public partial class managementsystem : Form { private const string fileName = @"C:\t

我有一个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文件中。更改txt文件名和文本框名称,您就可以开始了:)

掌握和。 在OnClick事件中使用流编写器或读取器打开新流。然后使用类似

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();
} 

试试看

让我试着澄清你的问题:

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

替换文本文件中的一行非常容易。只需将所有行读取到一个数组中,替换该行并将所有内容写回文件:

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 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));
}
private void Form1\u加载(对象发送方,事件参数e)
{
LoadFile();
}
私有void加载文件()
{
如果(!File.Exists(fileName))
{
writeAllines();
返回;
}
string[]lines=File.ReadAllLines(文件名);
if(lines.Length!=textboxs.Length)
{
//文件中的行数不合适,请创建一个新文件
writeAllines();
返回;
}
对于(int i=0;itb.Text));
}

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

当然可以。您正在读取所有文本并将其设置为所有文本框。另一方面,您每次都会使用最后一个文本框内容重写文件。不。。。这是没有必要的。。。只需找到一种方法来区分单个文件中的条目!为此,我认为xml文件最适合。@user1934845:您可以通过google找到许多代码,用于将每个文本框数据保存在单独的节点中,然后逐节点读取文本并将其放回相应的文本框sw.Close不需要,因为您有一个using语句。为什么您要使用StreamReader/StreamWriter?OP中的File.ReadAllText和File.WriteAllText执行相同的任务。实际上,您没有显示如何在文件中找到与文本框匹配的特定行。我尝试了这种方法,但它仍然覆盖了另一个文本框,因此它们最终都具有相同的文本
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));
}