Reputation: 61
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;
}
}
Upvotes: 3
Views: 4910
Reputation: 216
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 :-)
Upvotes: 2