2012-07-19 39 views
0

有了這個代碼:爲什麼我的CheckedListBox的ItemText被視爲「0」?

for (int i = 0; i <= (checkedListBoxPlatypi.Items.Count - 1); i++) 
{ 
    if (checkedListBoxPlatypi.GetItemCheckState(i) == CheckState.Checked) 
    { 
     ReturnListPlatypi.Add(ParsePlatypusID(checkedListBoxPlatypi.GetItemText(i))); 
    } 
} 

...和CheckedListBox是確實必須通過分配給它的文本(FriendlyPlatypus是內容的字符串):

checkedListBoxPlatypi.Items.Add(FriendlyPlatypus); 

... ParsePlatypusID( )正在傳遞「0」...?

回答

2

我假設你沒有將「我」添加到列表框中,所以不會有該對象的任何文本。你只需要直接在物體:

for (int i = 0; i <= (checkedListBoxPlatypi.Items.Count - 1); i++) 
{ 
    if (checkedListBoxPlatypi.GetItemCheckState(i) == CheckState.Checked) 
    { 
     ReturnListPlatypi.Add(ParsePlatypusID(checkedListBoxPlatypi.Items[i].ToString())); 
    } 
} 

如果,事實上,你已經通過數據綁定增加了一個對象,你想直接到達「DisplayMember」字段,你可以使用:

for (int i = 0; i <= (checkedListBoxPlatypi.Items.Count - 1); i++) 
{ 
    if (checkedListBoxPlatypi.GetItemCheckState(i) == CheckState.Checked) 
    { 
     ReturnListPlatypi.Add(ParsePlatypusID(checkedListBoxPlatypi.GetItemText(checkedListBoxPlatypi.Items[i]))); 
    } 
} 
+0

@J:不,「Friendly Platypus」是一個類似於「Platypi R Us(314)/(42)」的字符串,而不是一個枚舉(我嘗試追加.ToString()來進行盡職調查,但它沒有區別)。 – 2012-07-19 19:01:08

+0

@克萊香農編輯,因爲我顯然完全錯過了船。 – 2012-07-19 19:26:54

相關問題