WPF保存输入&;更新

本文关键字:更新 amp 保存 输入 WPF | 更新日期: 2023-09-27 18:28:43

我已经到了可以将所有输入保存到文本文件的地步,但现在我想创建另一个功能,在这里我可以更新该文件,但只能通过表单本身

private void button_Click(object sender, RoutedEventArgs e)
{
    string lb1 = "";
    string lb = "";
    //Removes listboxtitem object name
    ListBoxItem mySelectedItem0 = listBox.SelectedItem as ListBoxItem;
    ListBoxItem mySelectedItem1 = listBox1.SelectedItem as ListBoxItem;
    if (mySelectedItem0 != null && mySelectedItem1 != null)
    {
        lb1 = mySelectedItem1.Content.ToString();
        lb = mySelectedItem0.Content.ToString();
    }

    TextWriter saveText = new StreamWriter(@"D:'blah.txt");
    saveText.Write("===================================[Receipt]===================================" + "'r'n");
    saveText.Write("|Title:" + " " + comboBox.Text + "'r'n");
    saveText.Write("|Surname:" + " " + textBox1.Text + "'r'n");
    saveText.Write("|Forname:" + " " + textBox2.Text + "'r'n");
    saveText.Write("|Passanger[s]:" + " " +  textBox6.Text + "'r'n");
    saveText.Write("|Group:" + " " + checkBox.IsChecked + "  " + "No: " + textBox4.Text + "'r'n");
    saveText.Write("|Class:" + " " + comboBox1.Text + "'r'n");
    saveText.Write("|Luggage:" + " " + textBox7.Text + "'r'n");
    saveText.Write("|Suitcase:" + " " + checkBox1.IsChecked + "  " + "No: " +textBox5.Text + "'r'n");
    saveText.Write("|Departure On:" + "  " + DatePicker.Text + " " + "At:" + "  " +comboBox2.Text + "'r'n");
    saveText.Write("|Destination From:" + " " + lb + " " + "To:" + " " + lb1 + "'r'n");
    saveText.Write("===================================[Receipt]===================================");


    saveText.Close();
    File.AppendAllText(@"D:'blah.txt", Environment.NewLine);
}

所以在这里,它将所有输入和值作为字符串安全地保存到blah.txt文件中,现在我如何将文件中的所有信息检索到表单中并更新它?

WPF保存输入&;更新

您可以使用ReadAllText函数读取文本文件的全部内容。此外,你可以浓缩你的文本结构:

String ticketText = String.Format(@"
===================================[Receipt]====================================
|Title: {0}
|Surname: {1}
|Forname: {2} 
...
...
", comboBox.Text, textBox1.Text, textBox2.Text, ...);
saveText.Write(ticketText);

这种构建文本的方式更好,因为所有事情都是一次性完成的,而且更容易阅读。使用插值字符串时,这将更加容易。

此外,我建议按其含义命名控件:例如,txtTitleTitleText表示Title

在代码中,您使用两种不同的方法在一个进程中写入文件。您可以使用StreamWriter保存收据。然后静态File.AppendAllText附加一条空行。没必要那样做。我认为最好用单独的方法生成消息,然后使用静态File.AppendAllText一次性保存所有内容:

private string GetReceipt()
{
    StringBuilder receiptBuilder = new StringBuilder();
    receiptBuilder.AppendLine("===================================[Receipt]===================================");
    receiptBuilder.AppendLine("|Title:" + " " + comboBox.Text);
    receiptBuilder.AppendLine("|Surname:" + " " + textBox1.Text);
    receiptBuilder.AppendLine("|Forname:" + " " + textBox2.Text);
    receiptBuilder.AppendLine("|Passanger[s]:" + " " + textBox6.Text);
    receiptBuilder.AppendLine("|Group:" + " " + checkBox.IsChecked + "  " + "No: " + textBox4.Text);
    receiptBuilder.AppendLine("|Class:" + " " + comboBox1.Text);
    receiptBuilder.AppendLine("|Luggage:" + " " + textBox7.Text);
    receiptBuilder.AppendLine("|Suitcase:" + " " + checkBox1.IsChecked + "  " + "No: " + textBox5.Text);
    receiptBuilder.AppendLine("|Departure On:" + "  " + DatePicker.Text + " " + "At:" + "  " + comboBox2.Text);
    receiptBuilder.AppendLine("|Destination From:" + " " + lb + " " + "To:" + " " + lb1);
    receiptBuilder.AppendLine("===================================[Receipt]===================================");
    receiptBuilder.AppendLine();
    return receiptBuilder.ToString();
}

然后保存到文件:

File.AppendAllText(filePath, GetReceipt());

通过这个小的重构,您现在可以轻松地完成请求的第一部分:更新现有文件。

当您需要用新数据更新文件时,只需调用GetReceipt()并附加结果即可。

或者,如果你需要删除旧文件,那么你可以使用WriteAllText代替:

File.WriteAllText(filePath, GetReceipt());

现在是文件的读取。取决于你需要什么。如果你只是阅读文本并将其显示在文本框中,那么这相当容易。只需使用ReadAllText

myTextBox.Text = File.ReadAllText(filePath);

但是,如果您想在适当的组合框和文本框中显示它,则必须手动读取该文件。但如果是这样的话,那么我鼓励您稍微重构一下代码:

创建一个类,保存收据的所有信息。每次要保存数据时,都要创建一个此类的新实例,然后对其进行序列化(或保存到数据库中),以便检索特定字段。如果你仍然想有一种很好的方式来显示收据,你可以覆盖ToString方法。