468,545 Members | 1,892 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,545 developers. It's quick & easy.

Make string-array from checkboxlist

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 :)
Nov 2 '06 #1
2 6733
Got it:
//Make sting-Array:
string[] arrInterest = new string[1000];
for (int i = 0; i < chkInterests.Items.Count; i++)
{
if (chkInterests.Items[i].Selected)
{
arrInterest[i] = (string)chkInterests.Items[i].Text;
}
}
"Řyvind Isaksen" <ho***@egmont.nowrote in message
news:O$**************@TK2MSFTNGP03.phx.gbl...
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 :)

Nov 2 '06 #2
On Thu, 02 Nov 2006 10:39:48 +0100, Řyvind Isaksen <ho***@egmont.nowrote:
Got it:
//Make sting-Array:
string[] arrInterest = new string[1000];
for (int i = 0; i < chkInterests.Items.Count; i++)
{
if (chkInterests.Items[i].Selected)
{
arrInterest[i] = (string)chkInterests.Items[i].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<Tand possibly its ToArray

List<stringselection = 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

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by pkilambi | last post: by
42 posts views Thread by WindAndWaves | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.