简体   繁体   English

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

[英]How can i add items to listView to specific tab?

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

In the ProgressChanged event i'm adding the items to the listView(lstMail). 在ProgressChanged事件中,我将项目添加到listView(lstMail)。

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

But this line will keep adding also the DateSent to the first tab and not to the date tab: 但是此行还将继续将DateSent添加到第一个选项卡,而不是添加到日期选项卡: 在此输入图像描述

There is a subject tab and a date tab and i want that the part 有一个主题选项卡和一个日期选项卡,我想要那一部分

allMessages[countMsg].Headers.DateSent

Will be under the date tab. 将在日期标签下。

Change this line: 更改此行:

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

To: 至:

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

Hope this helps. 希望这可以帮助。

This is the standard way of adding items to columns in a listView. 这是将项目添加到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});

Also see: C# listView, how do I add items to columns 2, 3 and 4 etc? 另请参见: C#listView,如何将项目添加到第2、3和4列等中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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