【问题标题】:Get SubItem value when double-clicking on Item in ListView在ListView中双击Item时获取SubItem值
【发布时间】:2011-02-22 21:06:18
【问题描述】:

我有一个包含 2 列的列表视图,当我双击一个项目时,我需要在 TextBox 控件中显示其相应子项目的值。我该怎么做?

我搜索了 Google,但它没有返回任何有用的信息,可能是因为我不确定要搜索什么。

谢谢

【问题讨论】:

  • 不可能那么难...Windows 一直都在这样做!

标签: c# .net winforms listview subitem


【解决方案1】:

您要阅读的 MSDN 链接是 ListViewItemListViewSubItem
您可以通过 ListViewItem.SubItems 属性访问列表视图项的子项 要记住的最重要的事情是,第一个子项是指所有者列表视图项,因此要访问您需要从 1 开始索引的实际子项。这将返回一个 ListViewSubItem 对象,您可以获得它的文本通过调用ListViewSubItem.Text 字符串。


SubItems[0] 为您提供“父”列表视图项
SubItems[1] 为您提供第一个子项等

快速、讨厌的代码 sn-p

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
      ListView.SelectedIndexCollection sel = listView1.SelectedIndices;

      if (sel.Count == 1)
      {
          ListViewItem selItem = listView1.Items[sel[0]];
          textBox1.Text = selItem.SubItems[1].Text;
      }
}

希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2021-04-27
    相关资源
    最近更新 更多