2013-05-29 21 views
-3

以及我的代碼說明一切ListView中選定值C#

 if (listView1.SelectedItems[0].Text == "") 
     { 
      MessageBox.Show(listView1.SelectedItems[0].Text); 
      MessageBox.Show("Please Select Value First", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
     else 
     { 
     } 

,但我得到的圖片 http://i.stack.imgur.com/nOXMY.png

+0

你的代碼不能解釋everyting,你使用多選?或者你只是選擇1項? –

+0

用戶將只選擇1個項目 –

回答

1

解釋了這個錯誤。如果沒有任何選定的項目,那麼你就不能要求第一個(listView1.SelectedItems[0])。換句話說,SelectedItems是空的。

看起來你正在嘗試做這樣的事情。使用SelectedItems.Count檢查是否有在集合中任何東西:

// if there aren't any selected items 
if (listView1.SelectedItems.Count <= 0) 
{ 
    // then give an error 
    MessageBox.Show("Please Select Value First", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    return; 
} 
// otherwise proceed 
0

你必須設置至少1所選項目上的ListView之前嘗試檢索。 沒有什麼魔法可以爲你做。