通过列表框控件跟踪博客帖子

时间:2012-08-25 18:15:07

标签: c# arraylist checkedlistbox

对于错误的标题,我不知道如何描述它。

基本上,我正在阅读Wordpress博客的RSS源中的博客文章,然后将它们添加到CheckedListBox控件。

博客帖子信息(帖子标题和永久链接)存储在ArrayList中,如此:

// Store post data
ArrayList post = new ArrayList();
post.Add(this.nodeItem["title"].InnerText);
post.Add(this.nodeItem["link"].InnerText);

// Store item data in posts list
posts.Add(post);

然后将ArrayList posts返回到我的主窗体。我像这样填充CheckedListBox:

// Grab the latest posts
this.posts = rssReader.getLatestPosts();

// Loop through them and add to latest posts listbox
foreach (ArrayList post in posts)
{
    lbLatestPosts.Items.Add(post[0]);
}

运行之后,我的CheckedListBox显示帖子标题。我希望能够根据帖子URL解析信息,如果你记得的话是post[1]。但是,我无法做到这一点,因为我没有办法从CheckedListBox获取post[1]

我能想到的唯一方法是循环检查CheckedListBox中的每个项目,然后将帖子标题与posts中的元素进行比较。如果它们匹配,我将使用类似post = posts[index][1]的数组索引。

我一直告诉自己必须有一个更好的方法来做到这一点。有吗?

1 个答案:

答案 0 :(得分:0)

直接来自msdn example。像这样:

listView1.CheckBoxes = true;
listView1.View = View.Details;

//Set the column headers and populate the columns.
listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;

ColumnHeader columnHeader1 = new ColumnHeader();
columnHeader1.Text = "Title";
columnHeader1.TextAlign = HorizontalAlignment.Left;
columnHeader1.Width = 146;

listView1.Columns.Add(columnHeader1);

listView1.BeginUpdate();

foreach (ArrayList post in posts)
{
    string[] postArray = new string[] { post[0].ToString() };
    ListViewItem listItem = new ListViewItem(postArray);
    listItem.Tag = post;
    listView1.Items.Add(listItem);
}

//Call EndUpdate when you finish adding items to the ListView.
listView1.EndUpdate();

现在你知道如何从listView项中获取post[1]我想..来自Tag属性。但最后我会要求你取消ArrayLists ..

相关问题