以下是我對動態添加的CheckBox到CheckBoxList的代碼:動態添加的CheckBox到CheckBoxList的不顯示文本
foreach (WCore.CategoryFields cat in Global.getCategories())
{
CheckBox c = new CheckBox();
c.Text = cat.CategoryId;
c.Tag = cat.CategoryName;
if (ints != null)
{
if (ints.Contains(c.Tag))
Invoke(new Action(()=>checkedListBox1.Items.Add(c, true)));
else
Invoke(new Action(()=>checkedListBox1.Items.Add(c, false)));
}
else
Invoke(new Action(()=>checkedListBox1.Items.Add(c, false)));
}
的問題是,每當我運行此代碼,複選框被添加,但不與文本。就像這樣:
我試圖調試它,然後我發現,CheckBox實例「C」越來越文本,但不顯示它。
在這裏看到:
請告訴我什麼是錯在這段代碼怎麼回事?
UPDATE
請注意,我不能這樣使用它:
Invoke(new Action(()=>checkedListBox1.Controls.Add(c)));
因爲它能夠更好地使用面板,而不是CheckBoxList的然後。 此外,我想兩個值一個顯示爲文本和其他隱藏的價值爲每個複選框中的CheckBoxList
更新2
代碼獲取所選項目:
List<string> SelInts = new List<string>();
foreach (ListBoxItem c in checkedListBox1.SelectedItems)
{
SelInts.Add(c.Tag.ToString());
}
我想你的意思是你想讓'checked items'不是'selected items'? –
雅謝謝,改變那工作:) –