简体   繁体   中英

C# WinForms, Adding a usercontrol Dynamically during runtime and controls are invisible?

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;
            }
        }

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 :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM