Error when trying to populate listbox
Hi I am a student I am getting this error:
forreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerat开发者_JAVA百科or'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string result = " ";
foreach (string activity in listBox1.SelectedItems)
{
result += activity + " ";
}
this.textBox1.Text = result;
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox2.Text);
textBox2.Clear();
}
}
}
Pl. help me
Your code works fine for me. However, your code will break if you've added something other than a string
to your ListBox
's Items
collection (although this will produce a different error than what you're apparently seeing).
Can you add the code you use to populate this ListBox?
精彩评论