3

I'm working on a contact manager, and I want to add controls for contact details such as Phone Number, Email to a usercontrol. i created a user control called TextPrompt that includes a label and textbox. the code should sort through the contacts Info and add a control for each entree, the program produces no errors(logical, and syntax as far as i can tell). I ran checks to make sure the controls were being added to the panel after the loop ran and it shows the controls are there, but they arent visible during runtime.

        List<ContactType> details = contact.ReturnAllContactDetails();
        int y = 0;

        if (contact != null)
        {
            lbl_Name.Text = "";
            if (contact.GetContactValueByType("FirstName") != null) { lbl_Name.Text = contact.GetContactValueByType("FirstName") + " "; }
            if (contact.GetContactValueByType("LastName") != null) { lbl_Name.Text = lbl_Name.Text + contact.GetContactValueByType("LastName"); }
            if (contact.GetContactValueByType("Company") != null) { lbl_Name.Text = lbl_Name.Text + "\n" + contact.GetContactValueByType("Company"); }

            pnl_ContactDetails.BringToFront();
            pnl_ContactDetails.Controls.Clear();
            pnl_ContactDetails.SuspendLayout();
            for(int i = 3; i < details.Count; i++) {
                TextPrompt txtbox = new TextPrompt(details[i]);                 //Textbox to be added
                MessageBox.Show(details[i].value);
                this.pnl_ContactDetails.Controls.Add(txtbox);

                txtbox.Name = details[i].name;                                  //Sets properties
                txtbox.Location = new Point(25, y);
                txtbox.Size = new System.Drawing.Size(345, 45);
                txtbox.TextValueChanged += new EventHandler(txtbox_TextChanged);
                txtbox.Show();

                txtbox = (TextPrompt)this.pnl_ContactDetails.Controls[i - 3];   //Checks to make sure controls are on form.
                MessageBox.Show(txtbox.ContactDetails.name);

                y = y + 45;
            }
        }
King King
  • 61,710
  • 16
  • 105
  • 130
  • looks like your list `details` even doesn't have any element? you initialize it and let it there? Hence the `for` loop would never run. – King King Sep 03 '13 at 02:52
  • As mentioned in my post, I have already made sure the data is there along with the controls. The contact.ReturnAllContactDetails() function returns a already initialized List. – Justin Clevenger Sep 03 '13 at 03:16

1 Answers1

2

It seems that you have called SuspendLayout() without telling the panel to ResumeLayout()


pnl_ContactDetails.SuspendLayout();
for(int i = 3; i < details.Count; i++)
{
    TextPrompt txtbox = new TextPrompt(details[i]); //Textbox to be added
    MessageBox.Show(details[i].value);

    txtbox.Name = details[i].name; //Sets properties
    txtbox.Location = new Point(25, y);
    txtbox.Size = new System.Drawing.Size(345, 45);
    txtbox.TextValueChanged += new EventHandler(txtbox_TextChanged);
    /* txtbox.Show(); */ // Leave this call out in favor of:
    txtbox.Visible = true;
    this.pnl_ContactDetails.Controls.Add(txtbox);

    txtbox = (TextPrompt)this.pnl_ContactDetails.Controls[i - 3]; //Checks to make sure controls are on form.
    MessageBox.Show(txtbox.ContactDetails.name);

    y = y + 45;
}
pnl_ContactDetails.ResumeLayout();

I made a few modifications to your code. Caveat Emptor :-)

EtherealMonkey
  • 216
  • 4
  • 13