-3

I have been working a program that is used as a guide/way to memorize items when studying terms for whatever (test, exam, etc.). It generates a set amount of textboxes inside of a group box (which has the subject name set as its text property) in which you can write the term name and definition. I was wondering how I would save what was written in the text boxes after being generated. Perhaps being able to save the state of the application after pressing save and being able to set the application to its previous state when wanted (Kind of like snapshots that you use in virtual machines). Another way I thought of is to perhaps make a group of each subject somehow and within that store an array of text in each term name box and the associated term definition. Here is the code inside of the button I press to generate the text boxes. There is also a photo of the form: Photo of form Here is one of the program running: Image of Program running Edit: I am not asking for the straight up entire code. I would like just a guideline/idea of how I would go about doing this.

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

2 Answers2

1

Maybe you can try to save the textbox info into Settings.

First, go to Project -> Properties -> Settings and add new items(type of StringCollection) in Settings.

enter image description here

Then, modify the code like this(save the location of the TextBox in the format of "x;y"):

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

Besides, if you get the exception System.NullReferenceException: 'Object reference not set to an instance of an object.', try to add a default value for each "setting".


Update:

The way to set Settings default value.

enter image description here

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
  • Thank you for the answer! This is quite helpful, but I'm not really sure how to add a default value for each setting. It seems like it would be similar to this post: https://stackoverflow.com/questions/59222317/system-collections-specialized-stringcollection-settings-work-fine-in-debug-and but I cannot figure out how to do it. – BusterCody3 Nov 23 '20 at 22:45
  • @BusterCody3 I have updated the answer and posted "set default value" gif. – 大陸北方網友 Nov 24 '20 at 01:22
-1

I used to do something similar when storing the positions of forms and the size of some controls on them. Then it was necessary for the application to open on restart in the same form as it was last used.

All forms and controls data I saved to XML file, when closing application. When then application has been started I read this XML file and set positions of forms and controls.

v m
  • 195
  • 5