c#从TextBox和monthCalendar创建JSON
本文关键字:创建 JSON monthCalendar TextBox | 更新日期: 2023-09-27 18:06:18
现在我有了一个带有文本框和日历的WindowsForm,以及两个按钮(继续和完成)。用户必须在框中输入文本并选择日期。如果他按下continue,则必须将文本和日期写入json文件。然后,相同的窗口应该再次打开,他可以输入新的值。最后,如果他单击finish按钮,Values应该被写入文件,窗口可以关闭。(用户应该在输入第一个值后点击完成)。我已经创建了一个get/set类:
class Nachrichten_Felder
{
public string Nachrichten { get; set; }
public string Datum { get; set; }
}
我怎样才能得到这份工作?我真的不知道如何将文本和日期写入json并再次打开窗口…
好的,我给你做了作业。
using Newtonsoft.Json;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
//InitializeComponent();
var newsButton = new Button { Parent = this, Text = "Show" };
newsButton.Click += NewsButton_Click;
}
private void NewsButton_Click(object sender, EventArgs e)
{
DialogResult result;
do
{
using (var newsForm = new NewsForm())
result = newsForm.ShowDialog();
} while (result == DialogResult.OK);
}
}
class NewsForm : Form
{
TextBox newsTextBox;
MonthCalendar monthCalendar;
Button continueButton;
Button finishButton;
Nachrichten_Felder news;
public NewsForm()
{
Width = 400;
newsTextBox = new TextBox { Parent = this, Multiline = true, Size = new Size(200, 200) };
monthCalendar = new MonthCalendar { Parent = this, Location = new Point(220, 0), MaxSelectionCount = 1 };
continueButton = new Button { Parent = this, Text = "Continue", Location = new Point(200, 220), DialogResult = DialogResult.OK };
finishButton = new Button { Parent = this, Text = "Finish", Location = new Point(300, 220), DialogResult = DialogResult.Cancel };
news = new Nachrichten_Felder();
newsTextBox.DataBindings.Add("Text", news, "Nachrichten");
monthCalendar.DataBindings.Add("SelectionStart", news, "Datum");
this.FormClosing += NewsForm_FormClosing;
}
private void NewsForm_FormClosing(object sender, FormClosingEventArgs e)
{
string json = JsonConvert.SerializeObject(news, Formatting.Indented);
File.AppendAllText(@"news.txt", json);
}
}
class Nachrichten_Felder
{
public string Nachrichten { get; set; }
public DateTime Datum { get; set; }
}
}