3

我正在开发联系人管理器,我想将联系人详细信息(例如电话号码、电子邮件)添加到用户控件。我创建了一个名为 TextPrompt 的用户控件,其中包含一个标签和文本框。代码应该对联系人信息进行排序并为每个主菜添加一个控件,程序不会产生错误(据我所知,逻辑和语法)。我进行了检查以确保在循环运行后控件被添加到面板中,它显示控件在那里,但它们在运行时不可见。

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

1 回答 1

2

您似乎在SuspendLayout()没有告诉专家组的情况下打了电话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();

我对你的代码做了一些修改。买者自负 :-)

于 2013-09-03T05:41:46.343 回答