简体   繁体   中英

How to keep information in textboxes of a child form that was loaded in TableLayoutPanel?

I need to keep the information that was entered in textboxes even if the user opened another child form,

for example: let's say we have 2 forms to be loaded in the same cell of a TableLayoutPanel

  1. the user entered the required information BUT they clicked on a button to open another form for any reason.

2.when they back to continue then the form is new and empty then they have to re-enter all the information again.

how to avoid that, and please take a look at my code (does it need any modifications for the same purpose).

bool isChildAlready = false;
string childName = null;

private void button12_Click(object sender, EventArgs e)
        {
            frmEmp chld = new frmEmp();
            loadChild(chld);
        }
        private void loadChild(Form child)
        {
            if (isChildAlready)
            {
                if (child.Name != childName)
                {
                    this.tlbMain.Controls.RemoveAt(1);
                    child.TopLevel = false;
                    child.BackColor = this.tlbMain.BackColor;
                    child.Dock = DockStyle.Fill;
                    this.tlbMain.Controls.Add(child, 1, 0);
                    child.Show();
                    isChildAlready = true;
                    childName = child.Name;
                }
            }
            else
            {
                child.TopLevel = false;
                child.BackColor = this.tlbMain.BackColor;
                child.Dock = DockStyle.Fill;
                this.tlbMain.Controls.Add(child, 1, 0);
                child.Show();
                isChildAlready = true;
                childName = child.Name;
            }
        }

Thank you.

With the following code, you can add infinite Form to TableLayoutPanel .

And then return to the previous control by pressing the clear button .

I used a static class with a static list to hold the Forms .

add form

private void btnGetTaker_Click(object sender, EventArgs e)
{
   if (tableLayoutPanel1.Controls.Count > 0)
   {                  
        StoreData.Controls.Add(tableLayoutPanel1.GetControlFromPosition(1, 0));
        tableLayoutPanel1.Controls.RemoveAt(0);
   }
   frmEmp child = new frmEmp();
   child.TopLevel = false;
   tableLayoutPanel1.Controls.Add(child, 1,0);
   child.Show();
   child.FormClosing += T_FormClosing;
}
private void T_FormClosing(object sender, FormClosingEventArgs e)
{
     ComeBackForm();         
}

clear tablelayoutpanel

private void btnClear_Click(object sender, EventArgs e)
{
    ComeBackForm();
}

private void ComeBackForm()
{
   if (tableLayoutPanel1.Controls.Count > 0)
   {
       tableLayoutPanel1.Controls.RemoveAt(0);
       var child = StoreData.Controls.LastOrDefault();
       tableLayoutPanel1.Controls.Add(child);
        StoreData.Controls.Remove(child);
   }
}

and StoreControl.cs

public static class StoreData
{
    public static List<Control> Controls { get; set; } = new List<Control>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM