如何将项目添加到listView到特定选项卡?

时间:2016-02-16 16:05:49

标签: c# .net winforms

private int numberofallmessages = 0;
private int countMsg = 0;

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    OpenPop.Pop3.Pop3Client PopClient = new OpenPop.Pop3.Pop3Client();
    PopClient.Connect("mail", 110, false);
    PopClient.Authenticate("me", "me",
    OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);
    int messageCount = PopClient.GetMessageCount();
    numberofallmessages = messageCount;
    allMessages = new List<OpenPop.Mime.Message>(messageCount);
    for (int i = messageCount; i > 0; i--)
    {
        if (backgroundWorker1.CancellationPending == true)
        {
            e.Cancel = true;
            return;
        }
        allMessages.Add(PopClient.GetMessage(i));
        int nProgress = (messageCount - i + 1) * 100 / messageCount;
        backgroundWorker1.ReportProgress(nProgress, PopClient.GetMessageCount().ToString() + "/" + i);
    }
    PopClient.Disconnect();        
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    pbt.Value = e.ProgressPercentage;
    pbt.Text = e.ProgressPercentage.ToString() + "%";
    pbt.Invalidate();
    label8.Text = e.UserState.ToString();
    label8.Visible = true;
    lstMail.Items.Add(allMessages[countMsg].Headers.Subject + "     " + allMessages[countMsg].Headers.DateSent);
    countMsg += 1;
}

在ProgressChanged事件中,我将项目添加到listView(lstMail)。

lstMail.Items.Add(allMessages[countMsg].Headers.Subject + "     " + allMessages[countMsg].Headers.DateSent);

但是这一行会继续将DateSent添加到第一个选项卡而不是日期选项卡: enter image description here

有一个主题标签和一个日期标签,我想要那个部分

allMessages[countMsg].Headers.DateSent

将在日期标签下。

2 个答案:

答案 0 :(得分:0)

更改此行:

lstMail.Items.Add(allMessages[countMsg].Headers.Subject + "     " + allMessages[countMsg].Headers.DateSent);

要:

lstMail.Items.Add(new ListViewItem(new string[]
{
    "",                                                    //From Column
    allMessages[countMsg].Headers.Subject,                 //Subject Column
    allMessages[countMsg].Headers.DateSent.ToString()      //Date Column
}));

希望这有帮助。

答案 1 :(得分:0)

这是向listView中的列添加项目的标准方法。

ListViewItem item1 = new ListViewItem("Something");
item1.SubItems.Add("SubItem1a");
item1.SubItems.Add("SubItem1b");
item1.SubItems.Add("SubItem1c");

ListViewItem item2 = new ListViewItem("Something2");
item2.SubItems.Add("SubItem2a");
item2.SubItems.Add("SubItem2b");
item2.SubItems.Add("SubItem2c");

ListViewItem item3 = new ListViewItem("Something3");
item3.SubItems.Add("SubItem3a");
item3.SubItems.Add("SubItem3b");
item3.SubItems.Add("SubItem3c");

ListView1.Items.AddRange(new ListViewItem[] {item1,item2,item3});

另见: C# listView, how do I add items to columns 2, 3 and 4 etc?