2014-01-08 48 views
0

即時嘗試將項目從listview1傳遞給另一個listview2,我想檢查所選項目是否仍然不存在於listview2中。即時通訊嘗試使用循環獲取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); 
    }} 
+1

你的代碼沒有任何意義。您正在通過ListView1項目(使用du)進行交互,並將這些索引應用於ListView2(大概是空的)。也不清楚爲什麼依靠DoubleClick事件(它引用ListView,而不是一個項目),然後考慮特定項目(而不是檢查所有項目)。您應該依賴選定項目事件或僅檢查所有項目。請解釋一下你想要的:只要將所有項目從ListView1移動到ListView2?那麼選擇的項目呢? – varocarbas

+0

多數民衆贊成我的問題。我不知道如何提取listview2的索引,以便我可以將它與listview1中的選定項目進行比較 –

+0

我想檢查listview1中的項目是否已經存在於listview2中。 –

回答

2

我試圖從ListView1的傳遞一個項目到另一個listview2和我 要檢查,如果所選擇的項目仍然沒有出現在listview2。

我將簡化你的方法是這樣的:

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

我會做同樣的,雖然試圖遵循OP邏輯了一會兒,同時簡化了事情... ^^。 – Sinatr

+0

即時比較子項目。包含字符串不工作 –

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); 
    } 
} 
+1

測試將會是最後一次迭代的結果。 –

+0

@Sam Leach不,只有當項目出現在listview2中時,測試纔會出現錯誤,否則將遍歷listview2的每個項目並仍然爲真。 – TylerD87

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

,但我非常不確定...

相關問題