【问题标题】:Keeping track of blog posts through a listbox control通过列表框控件跟踪博客文章
【发布时间】:2012-08-20 21:47:31
【问题描述】:

抱歉标题有误,我不知道该怎么形容了。

基本上,我正在阅读来自 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]

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

【问题讨论】:

  • 你能想到一个列表视图吗?对于列表视图,每个项目都可以标记为一个对象,在您的情况下为post,因此现在您可以获得 post[0] 和 post[1]。
  • 我需要复选框来选择要从中解析信息的特定帖子。我需要在列表复选框中显示帖子标题,但是当我完成选择帖子并单击“解析”时,我需要知道帖子 URL。有没有办法将项目“标记”到CheckedListBox 中的对象?
  • 不,没有。但是您可以改为使用选中的列表视图..
  • 我选择了 ListView。但是,如何将项目添加为对象?当我这样做时:lvPostQueue.Items.Add(post);post 是一个对象)我收到一条错误消息,说它无法将其转换为字符串。

标签: c# arraylist checkedlistbox


【解决方案1】:

直接来自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..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    相关资源
    最近更新 更多