0

I'm working on a project at the moment and I'm trying to have it where every contact within the ArrayList has their own tab page. Within the tab page I want all their info to be displayed, to do this I'm trying to create labels that will have their information. I'm right now just at the point trying to get at least one label to appear, but it does not appear to be displaying at all. My code is below. Any help?

            int count = 0;

            foreach (clsContactHandler contact in clsGlobal.mContacts)
            {
                string tabName = contact.FirstName + " " + contact.LastName;

                Font font = new Font("Microsoft Sans Serif", 16.0f, FontStyle.Bold);
                TabPage contactPage = new TabPage(tabName);
                tabs.TabPages.Add(tabName);
                Label label = new Label();

                contactPage.Controls.Add(label);

                label.Font = font;
                label.AutoSize = true;
                label.Location = new System.Drawing.Point(16, 7);
                label.Name = "label" + count;
                label.Size = new System.Drawing.Size(43, 13);
                label.Text = "Name:";
                count++;
            }
Diligent Key Presser
  • 4,183
  • 4
  • 26
  • 34
Ben
  • 733
  • 8
  • 25
  • 1
    Doing it like that looks painful. You can simulate dynamic TabPages by putting all the controls in a UserControl and putting that in a TabPage as described here: http://stackoverflow.com/questions/17305249/how-can-i-duplicate-tabpage-in-c – Steve Wellens Nov 27 '14 at 05:19

2 Answers2

3

Since you're creating the new page as TabPage contactPage object,

tabs.TabPages.Add(tabName);

should be

tabs.TabPages.Add(contactPage);
Diligent Key Presser
  • 4,183
  • 4
  • 26
  • 34
0

like suggested use

tabs.TabPages[tabName].Controls.Add(label);

but before that set name property:

contactPage.Name="someUniqueName"

and use

tabs.TabPages[count].Controls.Add(label);

where count is I suppose, the index tracker in case TabPages["someUniqueName"] throws NullReferenceException.

Also, add contactpage to tabs and not tabName

Codeek
  • 1,624
  • 1
  • 11
  • 20