0
votes

Im fairly new to C#, I have a parent form (form1) with a tabcontrol. the first tab is static and i have some stuff in there. After that i can dinamicaly add embedded forms (all the same form2) on the tab with a button. The newly created forms each have a single textbox from which i want to retrieve the textboxname.Text and write it on a textbox outside the tabcontrol in form1..

in form2 i added public TextBox TextBox1 { get { return textBox1; } } and in form1 the private Form1 otherForm; ...

but i dont know where to go from there i think i have to do a foreach (TabPage tab in tabControl1.TabPages) but im not sure ^^

1
when you wanted to read the values of the textboxes in the tabs, you can find the parent control for the static form and then call the findcontrolsbyname and then iterate through the list and then get its values and display in the static page.Saravanan

1 Answers

1
votes

Try this out...the key is you need to CAST the control in the TabPage back to Form2 before you can access the property you added to it:

    private void button2_Click(object sender, EventArgs e)
    {
        if (tabControl1.SelectedTab != null)
        {
            if (tabControl1.SelectedTab.Controls.Count > 0)
            {
                if (tabControl1.SelectedTab.Controls[0] is Form2)
                {
                    Form2 f2 = (Form2)tabControl1.SelectedTab.Controls[0];
                    label1.Text = f2.TextBox1.Text;
                }
            }
        }
    }