[英]How to save state of Windows Form Application (or save data in text boxes with arrays)
我一直在开发一个程序,该程序在学习任何术语(测试、考试等)时用作记忆项目的指南/方式。 它在组框内生成一定数量的文本框(将主题名称设置为其文本属性),您可以在其中写入术语名称和定义。 我想知道如何在生成后保存在文本框中写入的内容。 也许能够在按下保存后保存应用程序的状态,并且能够在需要时将应用程序设置为以前的状态(有点像您在虚拟机中使用的快照)。 我想到的另一种方法可能是以某种方式将每个主题组成一组,并在其中存储每个术语名称框中的文本数组和相关术语定义。 这是我按下以生成文本框的按钮内的代码。 还有一张表格的照片: 表格的照片这是正在运行的程序之一: 程序运行的图像编辑:我不是要求直接完整的代码。 我只想要一个关于如何去做这件事的指导方针/想法。
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);
也许您可以尝试将文本框信息保存到设置中。
首先,转到Project -> Properties -> Settings
并在Project -> Properties -> Settings
添加新项目(type of StringCollection)
。
然后,像这样修改代码(以“x;y”的格式保存TextBox的位置):
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);
}
}
此外,如果您收到异常System.NullReferenceException: 'Object reference not set to an instance of an object.'
,尝试为每个“设置”添加一个默认值。
更新:
设置设置默认值的方式。
在存储表单的位置和一些控件的大小时,我曾经做过类似的事情。 然后应用程序必须以与上次使用相同的形式在重新启动时打开。
关闭应用程序时,我保存到 XML 文件的所有表单和控件数据。 当应用程序启动时,我读取这个 XML 文件并设置表单和控件的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.