foreach error?

Hi.. i am developing a windows application using c#. i have two list boxes. If the user selects an item in the list box and clicks on the button, the selected item should be in the selected listbox. I tried this code, but i am getting an error.

private void btnForwardOne_Click(object sender, EventArgs e)

{

if (lstAvailableGroups.SelectedItems.Count > 0)

{

lstSelectedGroups.Items.Clear();

foreach (ListBox item in lstAvailableGroups.SelectedItem)

{

lstSelectedGroups.Items.Add(item.ToString());

}

}

}

In the above code, i am getting error as: foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator' .

please help



Answer this question

foreach error?

  • Luke Yang

    You can still iterate on ListBox item, but you forgot an 's' on 'SelectedItems'.

    So you're trying to get a ListBox item from a single object and not a collection.

    -Larants-



  • Peter Richard

    just change the foreach statement as given below

    foreach (object item in lstAvailableGroups.SelectedItems)



  • foreach error?