2017-07-03 60 views
0

我知道如何項設置爲選中狀態:設置checkedListBox項檢查是否valuemember =東西

checkedListBox.SetItemChecked(index, true); 

,但我有,當我與int[] valueMembers參數打開的形式,它叫,所以我要檢查每一個值成員即=要檢查此參數。這是我曾嘗試:

public NovaPoruka(int[] primalacID) 
    { 
     InitializeComponent(); 

     foreach(CheckedListBox o in checkedListBox1.Items) 
     { 
      if(primalacID.Contains(Convert.ToInt32(o.SelectedValue))) 
      { 
       o.SetItemChecked(o.SelectedIndex, true); 
      } 
     } 
    } 

編輯: 我還沒有看到,我並沒有這樣做,所以它不會放棄錯誤之前初始化checkedListBox但現在,當我做了我在我滴錯誤CheckedListBox o in checkedListBox1.Items所以我做了一點改變,但仍然不知道如何獲得foreach循環內的當前項目的索引。這裏改變代碼:讓選定的指數

foreach(Int_String o in checkedListBox1.Items) 
{ 
    if(primalacID.Contains(Convert.ToInt32(o._int))) 
    { 
      checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true); 
    } 
} 

電流的方式返回我-1

+0

而且?問題是什麼?你忘了解釋爲什麼這個代碼不工作? – Steve

+0

它只是沒有。它不檢查它,並且不會丟失任何錯誤 –

+0

我已經做了一些編輯,因此它可能對您更容易 –

回答

0

我都做到了。這裏是最終代碼:

for(int i = 0; i < checkedListBox1.Items.Count; i++) 
{ 
    Int_String s = checkedListBox1.Items[i] as Int_String; 
    if(primalacID.Contains(s._int)) 
    { 
     checkedListBox1.SetItemChecked(i, true); 
    } 
} 
相關問題