我正在编写一个简单的电子邮件客户端,我有一个带有2个标签和2个面板和1个图片框的用户控件。但是创建这些用户控件并将其添加到flowlayoutpanel非常慢。例如,创建和添加20个这样的用户控件到面板需要大约10秒!我需要更快地完成这个过程。任何帮助表示赞赏。这是我的代码:
MessagesListPanel.SuspendLayout();
MessagesListPanel.Controls.Clear();
foreach (ImapX.Message message in MessagesLibrary.GetMessageList())
{
EmailListItem NewEmail = new EmailListItem(MessagesListPanel,
message.UId,
!message.Seen,
message.Attachments.Count() > 0,
message.From.DisplayName == null ? message.From.Address : message.From.DisplayName,
message.Subject == null ? "(No Subject)" : message.Subject,
message.UId == LastSelectedID);
NewEmail.Click += EmailItemClick;
NewEmail.FromLabel.Click += EmailItemClick;
NewEmail.SubjectLabel.Click += EmailItemClick;
NewEmail.AttachmentIcon.Click += EmailItemClick;
MessagesListPanel.Controls.Add(NewEmail);
}
MessagesListPanel.ResumeLayout();
EmailListItem的构造函数:
public EmailListItem(FlowLayoutPanel MPanel, long uid, bool IsUnread, bool HasAttachments, string From, string Subject, bool Selected)
{
InitializeComponent();
MessagesPanel = MPanel;
UID = uid;
if (IsUnread)
{
FromLabel.Font = new Font(FromLabel.Font, FontStyle.Bold);
SubjectLabel.Font = new Font(SubjectLabel.Font, FontStyle.Bold);
}
if (HasAttachments)
AttachmentIcon.Show();
FromLabel.Text = From;
SubjectLabel.Text = Subject;
if (Selected)
SelectMessage();
}
public void SelectMessage()
{
Selected = true;
this.BackColor = Color.FromArgb(85, 85, 88);
}
public void UnSelectMessage()
{
Selected = false;
this.BackColor = Color.FromArgb(65, 65, 68);
}