2013-01-09 208 views
0

什麼是檢查是否在CheckedListBox中檢查了任何內容的代碼。我正在申請註冊電影,我需要檢查從CheckedListBox電影的流派。如果沒有任何檢查,MessageBox應該會出現告訴你你需要爲電影選擇一個流派。檢查CheckedListBox?

回答

1

您應該檢查CheckedItemsCheckedIndices性質,這取決於你所需要的

CheckedListBox cl = new CheckedListBox(); 

if (cl.CheckedIndices.Count == 0) 
{ 
    MessageBox.Show("You need to select a Genre for the movie."); 
} 
0
if(checkedListBox1.CheckedItems.Count != 0) 
{ 
    // If so, loop through all checked items and print results. 
} 
else 
{ 
    MessageBox.Show("You need to select a Genre for the movie."); 
} 
1

你可以簡單地使用SelectedIndex屬性:

if(checkListBoxGenre.SelectedIndex == -1) 
{ 
    MessageBox.Show("You need to select a Genre for the movie."); 
} 

另一種選擇是使用Text property它獲取ListBox中當前選定項目的文本。

if(checkListBoxGenre.Text.Length == 0) 
{ 
    MessageBox.Show("You need to select a Genre for the movie."); 
} 

這只是一個可讀性和個人喜好的問題。

+0

我是這樣做的: else if(GenreCheck.Text.Length == 0) { MessageBox.Show(「請選擇電影的流派。」); } 它工作xD – user1957558

+1

@ user1957558:'Text'屬性返回第一個選定項目的文本。但是如果我想知道是否選擇了某個東西,我會使用'SelectedIndex'。這只是一個可讀性和個人喜好的問題。但編輯我的答案要考慮到這一點。 –

+0

@ user1957558檢查文字不好。更好地檢查我的答案中的兩個屬性中的一個 – VladL

相關問題