如何在列表视图中提取子项的索引?

时间:2014-01-08 13:59:30

标签: c# listview indexing

我试图将一个项目从listview1传递到另一个listview2,我想检查所选项目是否仍然不在listview2中。我试图使用for循环来获取listview2的每个索引,并将其与listview1中选择的项目进行比较。我尝试使用它,但错误表示值0不是有效索引。

private void listView1_DoubleClick(object sender, EventArgs e)
{
    bool test = true;
    for (int i = 0; i < listView1.Items.Count; i++)
    {
        string ls = listView2.Items[i].SubItems[0].Text;
        string ps = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
        if (ls.Trim() == ps.Trim())
        {
            test = false;
        }
    }
    if (test == true)
    {
        ListViewItem ty = new ListViewItem(listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text);
        ty.SubItems.Add(listView1.Items[listView1.FocusedItem.Index].SubItems[1].Text);
        listView2.Items.AddRange(new ListViewItem[] { ty });
    }
    else
    {
        MessageBox.Show("Student is already present in the list.","Cannot add to list",MessageBoxButtons.OK,MessageBoxIcon.Hand);
    }
 }

按照建议弄明白了。我使用了foreach声明

private void listView1_DoubleClick(object sender, EventArgs e)
{
 bool test = true;
 string ps = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;           
 foreach(ListViewItem disitem in listView2.Items)
 {
  string ls = disitem.SubItems[0].Text;
  if (ps.Trim() == ls.Trim())
  {
  test = false;
  }}
  if (test == true)
  {
  ListViewItem ty = new ListViewItem(listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text);                ty.SubItems.Add(listView1.Items[listView1.FocusedItem.Index].SubItems[1].Text);
  listView2.Items.AddRange(new ListViewItem[] { ty });
  }
  else
  {
  MessageBox.Show("Student is already present in the list.","Cannot add to list",MessageBoxButtons.OK,MessageBoxIcon.Hand);
  }}

3 个答案:

答案 0 :(得分:2)

  

我正在尝试将项目从listview1传递到另一个listview2和我   想要检查列表视图2中是否仍然没有所选项目。

我会将您的方法简化为:

private void listView1_DoubleClick(object sender, EventArgs e)
{
    // Get the value of the selected item
    string theItem = listView1.SelectedItems[0];

    // Add to second list if it's not already in there
    if(!listView2.Items.Contains(theItem))
    {
        listView2.Items.Add(theItem);
    }
    else
    {
        MessageBox.Show("Student is already present in the list.","Cannot add to list",MessageBoxButtons.OK,MessageBoxIcon.Hand);
    }
}

答案 1 :(得分:1)

private void listView1_DoubleClick(object sender, EventArgs e)
{
    bool test = true;
    var selectedItem = listView1.SelectedItems[0];

    foreach(var item in listview2.Items)
    {
         string listview2Text = item.SubItems[0].Text;
         string listview1Text = selectedItem.SubItems[0].Text;

         if (listview2Text.Trim() == listview1Text.Trim())
         {
            test = false;
         }
    }
    if (test == true)
    {
        //I am not sure exactly what you're trying to do if the test is true but I think you're trying to do this
        listView2.Items.Add(selectedItem);
    } 
    else
    {
        MessageBox.Show("Student is already present in the list.","Cannot add to list",MessageBoxButtons.OK,MessageBoxIcon.Hand);
    }
}

答案 2 :(得分:0)

我认为它应该是这样的:

private void listView1_DoubleClick(object sender, EventArgs e)
{
    bool add = true;
    string selected = listView1.Items[listView1.FocusedItem.Index];
    foreach(ListViewItem item in listView2.Items)
        if (selected.SubItems[0].Text == item.SubItems[0].Text)
        {
            add = false;
            break;
        }
    if(add)
        listView2.Items.Add(selected);
    else
        MessageBox.Show("Student is already present in the list.","Cannot add to list",MessageBoxButtons.OK,MessageBoxIcon.Hand);
}

但我很不确定......

相关问题