C# 如何保存Windows窗体应用程序的状态(或使用数组将数据保存在文本框中)
我一直在做一个程序,当学习任何术语(测试、考试等)时,它被用作记忆项目的指南/方法。它在一个组框内生成一定数量的文本框(将主题名称设置为其文本属性),您可以在其中写入术语名称和定义。我想知道生成后如何保存文本框中的内容。也许可以在按下save后保存应用程序的状态,并在需要时将应用程序设置为其以前的状态(类似于虚拟机中使用的快照)。我想到的另一种方法是,也许以某种方式将每个主题组成一组,并在每个术语名称框和相关术语定义中存储一个文本数组。下面是我按下按钮生成文本框的代码。还有一张表格的照片:这是一个正在运行的程序:Edit:我并不是要求提供完整的代码。我只想知道我将如何做这件事的指导方针/想法C# 如何保存Windows窗体应用程序的状态(或使用数组将数据保存在文本框中),c#,winforms,C#,Winforms,我一直在做一个程序,当学习任何术语(测试、考试等)时,它被用作记忆项目的指南/方法。它在一个组框内生成一定数量的文本框(将主题名称设置为其文本属性),您可以在其中写入术语名称和定义。我想知道生成后如何保存文本框中的内容。也许可以在按下save后保存应用程序的状态,并在需要时将应用程序设置为其以前的状态(类似于虚拟机中使用的快照)。我想到的另一种方法是,也许以某种方式将每个主题组成一组,并在每个术语名称框和相关术语定义中存储一个文本数组。下面是我按下按钮生成文本框的代码。还有一张表格的照片:这是一
GroupBox groupBox1 = new GroupBox();
TextBox textTest = new TextBox();
textTest.Location = new Point(15, 40);
groupBox1.Controls.Add(textTest);
Button buttonForBoxes = new Button();
NumericUpDown numberUpDown1 = new NumericUpDown();
groupBox1.Controls.Add(buttonForBoxes);
buttonForBoxes.Location = new Point(140, 40);
buttonForBoxes.Text = "moretext";
numberUpDown1.Location = new Point(15, 15);
groupBox1.Controls.Add(numberUpDown1);
groupBox1.AutoSize = true;
var numVal = numericUpDown1.Value;
var numDo2 = 40;
var numDo1 = 120;
var inSubjectBox = subjectBox.Text;
//Makes boxes however many times you specify
for (int i = 0; i < numVal; i++)
{
numDo2 += 110;
TextBox text1 = new TextBox();
text1.Location = new Point(15, numDo1);
groupBox1.Controls.Add(text1);
numDo1 += 110;
TextBox textThing = new TextBox();
textThing.Location = new Point(15, numDo2);
textThing.Multiline = true;
textThing.Size = new System.Drawing.Size(600, 60);
groupBox1.Controls.Add(textThing);
}
// Set the Text and Dock properties of the GroupBox.
groupBox1.Text = inSubjectBox;
groupBox1.Dock = DockStyle.Top;
// Enable the GroupBox (which disables all its child controls)
groupBox1.Enabled = true;
// Add the Groupbox to the form.
this.Controls.Add(groupBox1);
GroupBox-groupBox1=新的GroupBox();
TextBox textTest=新建TextBox();
textTest.Location=新点(15,40);
groupBox1.Controls.Add(textTest);
Button ButtonBoxes=新按钮();
NumericUpDown numberUpDown1=新的NumericUpDown();
groupBox1.Controls.Add(按钮框);
按钮框。位置=新点(140,40);
buttonBoxes.Text=“moretext”;
numberUpDown1.位置=新点(15,15);
groupBox1.Controls.Add(numberUpDown1);
groupBox1.AutoSize=true;
var numVal=numericUpDown1.Value;
var numDo2=40;
var numDo1=120;
var insobjectbox=subjectBox.Text;
//无论指定多少次,都会生成框
对于(int i=0;i
我以前在存储表单的位置和一些控件的大小时也做过类似的事情。然后,应用程序必须在重新启动时以上次使用的相同形式打开
关闭应用程序时,我保存到XML文件中的所有表单和控件数据。当应用程序启动后,我读取此XML文件并设置窗体和控件的位置。也许您可以尝试将文本框信息保存到设置中 首先,转到
Project->Properties->Settings
,在Settings中添加新项目(StringCollection类型)
然后,修改如下代码(以“x;y”格式保存文本框的位置):
private void Addtextbox\u单击(对象发送者,事件参数e)
{
Properties.Settings.Default.text1Collection.Clear();
Properties.Settings.Default.textThingCollection.Clear();
var numVal=2;
//代码省略
// ...
对于(int i=0;i
此外,如果您得到异常System.NullReferenceException:“对象引用未设置为对象的实例”。
,请尝试为每个“设置”添加默认值
更新: 设置设置默认值的方法
感谢您的帮助。您可以通过编写代码来完成这项工作。我们不会为你写那些代码的伊恩肯普嗯。。。我来这里是因为我不知道如何编写代码。我看到了大量的帖子,其中代码是作为答案编写的。只是因为我的账户有多新,你才说不是这样。我到处都找过如何做,但我不能
private void Addtextbox_Click(object sender, EventArgs e)
{
Properties.Settings.Default.text1Collection.Clear();
Properties.Settings.Default.textThingCollection.Clear();
var numVal = 2;
// code omitted
// ...
for (int i = 0; i < numVal; i++)
{
numDo2 += 110;
TextBox text1 = new TextBox();
text1.Location = new Point(15, numDo1);
groupBox1.Controls.Add(text1);
// save info to Settings
Properties.Settings.Default.text1Collection.Add(String.Format("{0};{1}", text1.Location.X, text1.Location.Y));
numDo1 += 110;
TextBox textThing = new TextBox();
textThing.Location = new Point(15, numDo2);
textThing.Multiline = true;
textThing.Size = new System.Drawing.Size(600, 60);
groupBox1.Controls.Add(textThing);
// save info to Settings
Properties.Settings.Default.textThingCollection.Add(String.Format("{0};{1}", textThing.Location.X, textThing.Location.Y));
// call Save()
Properties.Settings.Default.Save();
}
// code omitted
// ...
}
private void LoadtextboxFromSettings_Click(object sender, EventArgs e)
{
foreach (string text1str in Properties.Settings.Default.text1Collection)
{
TextBox text1 = new TextBox
{
Location = new Point(Convert.ToInt32(text1str.Split(';')[0]), Convert.ToInt32(text1str.Split(';')[1]))
};
groupBox1.Controls.Add(text1);
}
foreach (string textThingstr in Properties.Settings.Default.textThingCollection)
{
TextBox textThing = new TextBox
{
Multiline = true,
Location = new Point(Convert.ToInt32(textThingstr.Split(';')[0]), Convert.ToInt32(textThingstr.Split(';')[1])),
Size = new Size(600, 60)
};
groupBox1.Controls.Add(textThing);
}
}