c# - Dynamically added CheckBoxes to a CheckBoxList not showing Text -
following code adding checkboxes dynamically 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))); }
the problem whenever run code, checkboxes added not text. this:
i tried debug , found checkbox instance 'c' getting text not showing it.
see here:
please tell me what's wrong going on in code?
update
please note can't use this:
invoke(new action(()=>checkedlistbox1.controls.add(c)));
because better use panel instead of checkboxlist then. want 2 values 1 shown text , other hidden value each checkbox in checkboxlist
update 2
code selected items:
list<string> selints = new list<string>(); foreach (listboxitem c in checkedlistbox1.selecteditems) { selints.add(c.tag.tostring()); }
try this:
foreach (wcore.categoryfields cat in global.getcategories()){ listboxitem c = new listboxitem; 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))); } //add class somewhere in form class or beside public class listboxitem { public string text {get;set;} public object tag {get;set;} public override string tostring(){ return text; } }
i doubt somehow checkbox
can't displayed string although should show system.windows.forms.checkbox
instead of blanks.
Comments
Post a Comment