hello
I am writting a web browser with tabs
I have a problem in Accessing Controls in the tab which created on runtime
see this Code

private void button4_Click(object sender, EventArgs e)
        {
            TabPage t = new TabPage();
            t.BackColor = System.Drawing.Color.AliceBlue;
            t.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            // webBrowser1
            // 
            string name = "http://" + sitename.Text;
            WebBrowser wb = new WebBrowser();
            wb.Location = new System.Drawing.Point(7, 58);
            wb.MinimumSize = new System.Drawing.Size(20, 20);
            wb.Name = "webBrowser1";
            wb.Size = new System.Drawing.Size(660, 346);
            wb.TabIndex = 0;
            // bt1
            // 
            Button bt1 = new Button();
            bt1.Location = new System.Drawing.Point(371, 29);
            bt1.Name = "bt1";
            bt1.Size = new System.Drawing.Size(75, 23);
            bt1.TabIndex = 1;
            bt1.Text = "Start";
            bt1.UseVisualStyleBackColor = true;
            // 
            // bt2
            // 
            Button bt2 = new Button();
            bt2.Location = new System.Drawing.Point(471, 29);
            bt2.Name = "bt2";
            bt2.Size = new System.Drawing.Size(75, 23);
            bt2.TabIndex = 2;
            bt2.Text = "Pause";
            bt2.UseVisualStyleBackColor = true;
            // 
            // bt3
            // 
            Button bt3 = new Button();
            bt3.Location = new System.Drawing.Point(574, 29);
            bt3.Name = "bt3";
            bt3.Size = new System.Drawing.Size(75, 23);
            bt3.TabIndex = 3;
            bt3.Text = "End";
            bt3.UseVisualStyleBackColor = true;
            // 
            // lb1
            // 
            Label lb1 = new Label();
            lb1.AutoSize = true;
            lb1.Location = new System.Drawing.Point(20, 12);
            lb1.Name = "lb1";
            lb1.Size = new System.Drawing.Size(40, 16);
            lb1.TabIndex = 4;
            lb1.Text = "name";
            // 
            // lb2
            // 
            Label lb2 = new Label();
            lb2.AutoSize = true;
            lb2.Location = new System.Drawing.Point(40, 34);
            lb2.Name = "lb2";
            lb2.Size = new System.Drawing.Size(33, 16);
            lb2.TabIndex = 5;
            lb2.Text = "num";
            //
            t.Controls.Add(lb1);
            t.Controls.Add(lb2);
            t.Controls.Add(bt1);
            t.Controls.Add(bt2);
            t.Controls.Add(bt3);
            t.Controls.Add(wb);
            t.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            t.Location = new System.Drawing.Point(4, 22);
            t.Padding = new System.Windows.Forms.Padding(3);
            t.Size = new System.Drawing.Size(678, 421);
            t.Name = i.ToString();
            t.Text = sitedes.Text;
            tabControl1.TabPages.Add(t);
            i++;
            int m = (int.Parse(numopen.Text)+1);
            numopen.Text = m.ToString();
            numrun.Text = m.ToString();
           try { wb.Navigate(new Uri(name)); }
            catch (System.UriFormatException er) { MessageBox.Show(er.ToString()); }
            sitename.Text = "";
            sitedes.Text = "";
        }

sitename is a textbox on first tab and sitedes so on
the problem as i said i can not know what controls in each tab do and its data
how i can make such as

string xv = tabControl1.TabIndex(2).lb2.Text;

But Not valid experation
Please help me in that problem
thanks
EraMaX

Recommended Answers

All 10 Replies

to get tab text

string tabText = tabControl1.TabPages[2].Text;

to get tab text

string tabText = tabControl1.TabPages[2].Text;

Hello RamyMahrous,
I am also working on a similar problem. But your solution is for getting the tabPage.text. In fact eramax's problem is about getting the text property of the controls(textboxex or labels) on the tabpages of tabcontrol. May be
string xv = tabControl1.TabPage[2].textbox.Text;
or somthing like this. Could you get the clue.
This will solve my problem too. Thanks in advance.

loool this thread was on April :) but I don't understand your problem. Clarify please :)

Check the controls within the tabpage, for the one you want.. Of course if you made a descendant of the tab page, that you then instanciated to put on your page, you would preknow what its called as its all setup in the class..

Hello RamyMahrous, LizR
Thanks for the response.
I am posting my code below, to give you better idea. I want to iterate through dynamically created tabPages and get values from textbox and combobox into an array.

public partial class Form1 : Form
    {
        // Dynamic controls on each tabPage 
        TabPage myTabPage;
        Label lblCount;
        TextBox txtCount;
        Label lblType;
        ComboBox cmbType;
        
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            tabControl1.TabPages.Clear();
        }
        public void AddTabPageWithControls()
        {
            //create new tabPage 
            string title = "TabPage" + (tabControl1.TabCount + 1).ToString();
            myTabPage = new TabPage(title);
            myTabPage.CausesValidation = true;

            // Add labels, textbox, and combobox to the tabpage
            lblCount = new Label();
            lblCount.Location = new Point(29, 55);
            lblCount.Text = "Description";
            lblCount.Font = new Font(lblCount.Font.FontFamily, 9);
            myTabPage.Controls.Add(lblCount);

            txtCount = new TextBox();
            txtCount.Location = new Point(129, 55);
            txtCount.Width = 100;
            myTabPage.Controls.Add(txtCount);

            lblType = new Label();
            lblType.Location = new Point(300, 55);
            lblType.Text = "HC Pannels";
            lblType.Font = new Font(lblType.Font.FontFamily, 9);
            myTabPage.Controls.Add(lblType);

            cmbType = new ComboBox();
            cmbType.Width = 50;
            cmbType.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbType.DataSource = TypeData();
            cmbType.Location = new Point(129, txt40Hold.Bottom + 16);
            myTabPage.Controls.Add(cmbType);

            //Add the tabpage to tabControl
            tabControl1.TabPages.Add(myTabPage);
        }

        //list of choice in combobox
        private ArrayList TypeData()
        {
            ArrayList typeList = new ArrayList();
            typeList.Add("A");
            typeList.Add("B");
            typeList.Add("C");
            return typeList;
        }

        private void btnNewInput_Click(object sender, EventArgs e)
        {
            AddTabPageWithControls();
            this.tabControl1.SelectedIndex = tabControl1.TabCount - 1;
        }

        private void btnSaveAllInputs_Click(object sender, EventArgs e)
        {  
            int totalInputs = new int[tabControl1.TabCount, 2];
            
            //Here I want to iterate through the tabPage and get values from textbox and combobox, into array totalInput
            // somthing like
            for (int i = 0; i <= tabControl1.TabCount - 1; i++)
            {
                totalInputs[i, 0] = tabControl1.tabPages[i].txtCount.text.toInt32();
                totalInputs[i, 1] = tabControl1.tabPages[i].cmbType.index;
            }
        }

    }

Code tags woulld have been nice

I still stand by my earlier statement of if you made a descendant of tab page you could have a class and it would then be easier to work with as you would have labels etc directly to work with

LizR,
You mean combining controls to create a composite control?
I could find one solution that seems to be working. I am still testing it.
Code:

private void btnSaveAllInputs_Click(object sender, EventArgs e)
        {
            totalInputs = new string[tabControl1.TabCount, 2];
            
            //Iterate through the tabPage and get values from textbox and combobox, into array totalInput
            for (int i = 0; i < tabControl1.TabCount; i++)
            {
                foreach (Control control in tabControl1.TabPages[i].Controls)
                {
                    if (control.GetType() == typeof(TextBox))
                        totalInputs[i, 0] = control.Text;
                    if (control.GetType() == typeof(ComboBox))
                        totalInputs[i, 1] = control.Text;
                }
            }
        }

Take this pesudo code
for each tab page
Just get control either they are TextBox or ComboBox
array[incremented index] <- TextBox -> .Text property
[incremented index] <- [incremented index] +1
array[incremented index] <- ComboBox->. SelectedItem Proprerty

haa? I think it's better than C# code

It doesnt have to be a composite control in the way you're thinking, but just a class in your code you can call instead of new TabPage, because a little like your procedure it would have the components, but they would be public properties etc, of the new class, and you could then call them by name, easily.

Such code for example:

_TabControl TabControl = new TabControl();

==========

this._TabControl.Controls.Add(new TabPage());
this._TabControl.SelectedIndex = _TabControl.Controls.Count-1;

 _TabControl.TabPages[_TabControl.SelectedIndex].Controls.Add(new WebBrowser());

=========

((WebBrowser)_TabControl.TabPages[_TabControl.SelectedIndex].Controls[0]).GoForward();
((WebBrowser)_TabControl.TabPages[_TabControl.SelectedIndex].Controls[0]).Navigate("http://bsuir.by");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.