我正在开发联系人管理器,我想将联系人详细信息(例如电话号码、电子邮件)添加到用户控件。我创建了一个名为 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;
}
}