Peiper
Peiper

Reputation: 56

TabPage with programmaticaly generated TextBoxes and ListView

I want to create a new TabPage on my TabControl. The new TabPage generates a new ListView and 3 TextBoxes. And I want to insert content to this. My problem: I don't know how to access the ListViews and TextBoxes for every new TabPage.

Here is my code:

// Create new Tab on Tabcontroll


private void createNewTabwithNotebookName()
        {
            TabPage myTabpage = new TabPage(NewNotebook.notebookname);
            tcMainWindow.TabPages.Add(myTabpage);
        }

 public void CreateListviewAndTextboxes()
        {

            ListView listView1 = new ListView();
            listView1.Bounds = new Rectangle(new Point(10, 10), new Size(159, 400));
            listView1.View = View.List;
            listView1.LabelEdit = false;
            listView1.AllowColumnReorder = true;
            listView1.CheckBoxes = false;
            listView1.FullRowSelect = true;
            listView1.GridLines = true;
            ColumnHeader column1 = new ColumnHeader();
            column1.Width = 159;
            column1.TextAlign = HorizontalAlignment.Left; 
            listView1.Columns.Add(column1);
            int i = tcMainWindow.TabCount - 1;

            // Add the ListView to the control collection.
            this.tcMainWindow.TabPages[i].Controls.Add(listView1);


            TextBox textbox1 = new TextBox();
            textbox1.Bounds = new Rectangle(new Point(240, 20), new Size(350, 20));
            textbox1.Multiline = true;
            this.tcMainWindow.TabPages[i].Controls.Add(textbox1);

            TextBox textbox2 = new TextBox();
            textbox2.Bounds = new Rectangle(new Point(240, 60), new Size(350, 250));
            textbox2.Multiline = true;
            this.tcMainWindow.TabPages[i].Controls.Add(textbox2);

            TextBox textbox3 = new TextBox();
            textbox3.Bounds = new Rectangle(new Point(240, 350), new Size(350, 20));
            textbox3.Multiline = true;
            this.tcMainWindow.TabPages[i].Controls.Add(textbox3);

}

Every ListView/TextBox should get other content. Please help.

Upvotes: 0

Views: 229

Answers (1)

Hassan
Hassan

Reputation: 5430

Try this code:

Loop through TabPages controls and find your desired controls:

//method for all tab pages 
private void AllTabPages()
{
      foreach (TabPage pg in tcMainWindow.TabPages)          
          FillControls(pg);    
}

//method for individual tab page
private void FillControls(TabPage pg)
{
     foreach (Control c in pg.Controls)
     {
           if (c is ListView)
           {
               //do something
               ListView lv = c as ListView;
               lv.Items.Add("abc");
               lv.Items.Add("def");
            }
            else if (c is TextBox)
            {
               //do something
               c.Text = "Add Some Text";
            }
      }
}

If you want to search control within specific tab page call FillControls(TabPage pg) method:

FillControls(this.tcMainWindow.TabPages[i]);

Upvotes: 1

Related Questions