.NET Framework - How to Populate CheckBoxList from Code

Asked By Bishop on 13-May-08 01:13 PM
I'm trying to find out how to populate a CheckBoxList from code without
using a database.

The following code generates 6 unchecked checkboxes without any text.
Dim x As Integer

For x = 0 To 5

Dim myCheckbox As New CheckBox

myCheckbox.Text = "Test " & x

myCheckbox.Checked = True

chkProfiles.Items.Add(myCheckbox)

Next

Any thoughts?




zack replied on 14-May-08 12:57 AM
Did you even check the documentation?

CheckBoxList defined:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxli=
st(vs.85).aspx
Bishop replied on 13-May-08 02:54 PM
No because I could not find any for windows application.

Should have mentioned previously, VB.NET 2005, windows application.


Did you even check the documentation?

CheckBoxList defined:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist(vs.85).aspx
zack replied on 14-May-08 12:57 AM
The documentation implies that the CheckBoxList control is not support
in WinForms, just Web apps.
KerryMoorma replied on 13-May-08 03:23 PM
Bishop,

Isn't the CheckboxList a web control?

For a windows forms application, perhaps you can use the CheckedListBox
control.

Kerry Moorman
kimiraikkonen replied on 14-May-08 12:57 AM
Bishop,
If your difficulty is to populate checkedListBox(this control is for
winforms) up to 6 items with text, i hope this is what you want:


' Here chkProfiles is your checkedListbox control
For x As Integer =3D 0 To 5
chkProfiles.Items.Add("Test" & x)
Next

Thanks,

Onur G=FCzel
Bishop replied on 13-May-08 03:50 PM
Yes, this is what I was looking for, I didn't realise it was different in
from ASP to Winform.  I wanted to add checkbox controls so I could control
text, if it was checked or not, the value (the not seen one), and the color
of the checkbox.




Bishop,
If your difficulty is to populate checkedListBox(this control is for
winforms) up to 6 items with text, i hope this is what you want:


' Here chkProfiles is your checkedListbox control
For x As Integer = 0 To 5
chkProfiles.Items.Add("Test" & x)
Next

Thanks,

Onur G├╝zel