Make string-array from checkboxlist

  • Thread starter Thread starter Øyvind Isaksen
  • Start date Start date
Ø

Øyvind Isaksen

Mabye this is question is very simple, but I need to make a string-array (in
a variable) with the values from a checkbox-list.

THANKS :)
 
Got it:


//Make sting-Array:
string[] arrInterest = new string[1000];
for (int i = 0; i < chkInterests.Items.Count; i++)
{
if (chkInterests.Items.Selected)
{
arrInterest = (string)chkInterests.Items.Text;
}
}
 
Got it:


//Make sting-Array:
string[] arrInterest = new string[1000];
for (int i = 0; i < chkInterests.Items.Count; i++)
{
if (chkInterests.Items.Selected)
{
arrInterest = (string)chkInterests.Items.Text;
}
}


This will create an unecessary long list with plenty of empty lines.

To limit the size to no more than the size of the CheckBoxList use

string[] arrInterest = new string[chkInterests.Items.Count];

But even this will create empty spaces. To create a size of only selected
items use a List<T> and possibly its ToArray

List<string> selection = new List<string>();
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
selection.Add(li.Text);
}

Swap List with ArrayList if you are using framework 1.1
 
Back
Top